In this article we’ll present and compare two functions which offer us a possibility to check whether an item exists in a given list. These functions are Exists and Contains, both available in System.Collections.Generic namespace. Provided examples are for two different lists: one containing integers and other one containing objects of custom created class. List … Continue reading C# – How to check if an item exists in a list
Category: How To
C# – How to convert string to enum
In this article we’ll explain the way of converting string to any enumeration type. There are two easiest ways to do so: using Parse or TryParse method. Examples of both are presented below. If you need to read how to create and use enums, please see following page: How to use enum. All examples will … Continue reading C# – How to convert string to enum
C# – How to use enum
Enum is value type containing a set of constants representing associated integers. These integers can be assigned automatically, starting from 0 and incremented by 1 on consecutive items, or assigned directly by programmer. This article presents the way of implementing our own enum and also examples of its usage. Enumeration type can be defined by … Continue reading C# – How to use enum
C# – How to initialize an array
Array is a fixed size collection of variables representing the same type. Array can store both built-in primitive types (like integer) and custom objects. In this article we’ll present different ways of initializing such arrays. Array of integers Let’s consider an array of integers. Below are presented five different methods of creating 3 elements array, … Continue reading C# – How to initialize an array
C# – How to generate random password
This article delivers proposal of algorithm to generate random passwords or actually random strings which are generated in safe manner and can be used as passwords, discount codes etc. Also, it is configurable, so that you can easily define expected length of string, pool of available characters or minimal number of occurrences of particular elements. … Continue reading C# – How to generate random password
C# – How to generate random string
There are situations where you need a random string which is built with specific characters. In this article you’ll see how to generate such string where both length and elements are totally up to you. Algorithm is very simple. It uses for loop, so that in every iteration it extends an output string with randomly … Continue reading C# – How to generate random string
C# – How to generate random number
Generating random numbers in C# is quick and easy using Random class. It’s built-in functionality which allows to produce integers, doubles and bytes. In this article you can find examples how to use it. Random integers The most basic usage is calling Next function without any parameters against an object of Random class. It returns … Continue reading C# – How to generate random number
C# – How to get unique items from list
The easiest way of getting unique items from list is LINQ’s Distinct() method. In this article you’ll see how to use it with both built-in types (like collection of integers) and custom types (like collection of complex type objects). Get unique values from collection of integers Let’s start with creating example list of items where … Continue reading C# – How to get unique items from list
C# – How to split a string
String can be split into collection of substrings based on a defined character (separator) using Split() function. Split is part of String class which exists in the System namespace. Split function is overloaded and has various forms which allow to: pass separator as a single character, array of characters, single string or array of strings; … Continue reading C# – How to split a string
C# – How to convert string to int
There are three easiest ways which allow you to convert string data type to int: Parse method TryParse method Convert class In the following article I’ll explain each one and show the example of its usage. Parse method Basic form of Parse method receives string as an input parameter and returns converted value as an … Continue reading C# – How to convert string to int