Average is a LINQ functionality which calculates average value of given collection. It works with both collections of primitive values and complex objects. Below article provides example of each one. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 var items = new List<int>() { 1, 2, 3, … Continue reading C# – LINQ Average Examples
C# – LINQ Sum Examples
Sum is a LINQ functionality which calculates total number of numeric items of a collection. In this article you can find examples of usage with list of primitive types (in our case integers) and also list of complex types. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 … Continue reading C# – LINQ Sum Examples
C# – How to check if an item exists in a list
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
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# – LINQ GroupBy Examples
GroupBy is a LINQ functionality which allows to group items from a collection based on a given key. It’s an equivalent to SQL’s GROUP BY clause. In this article we’ll show various examples of usage, starting from the simplest grouping by one key, through grouping by multiple keys, custom result selector and all of that … Continue reading C# – LINQ GroupBy Examples
C# – LINQ Join Examples
Join is a LINQ functionality to combine two collections and produce a single result set. Connection happens by comparing items from both series. When there is a match then such pair is one of the output elements. Let’s consider following example. We’ve got two separate collections: countries and cities, which store objects of Country and … Continue reading C# – LINQ Join Examples
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