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
Author: csharp.academy
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
Favour Boolean assignment via expressions in C# rather than if/else blocks
Where applicable favour Boolean assignment via expressions rather than if/else blocks. The first example below is still so common but the 2nd and 3rd examples require much less code without sacrificing readability. Note… Visual Studio has a nice refactor to help converting relevant if/else blocks to an expression.
Use the digit separator character in C# to improve readability of large numbers
Using the digit separator character _ in C# to break up your large numbers is just another little thing you can do to help the next developer (and you later) read your code. It’s available from C#7…
Nested ternarys can be hard to read for other developers
Be careful with nested ternarys as they can impact code readability. For example I know what 👇🏻 … var comparison = (a > b) ? “a is greater than b” : (a < b) ? “b is greater than a” : “a is equal to b”; does but I have to ‘think’ about it. I … Continue reading Nested ternarys can be hard to read for other developers
How to get started with BenchmarkDotNet using a simple example
BenchmarkDotNet has become the de facto standard for performance benchmarking on .NET. Nearly every video or blog post which Microsoft developers do which has some performance benchmarking uses it. It’s really easy to get started… 1 – Install BenchmarkDotNet from NuGet2 – Create a class which will contain your methods3 – Use the [Benchmark] attribute … Continue reading How to get started with BenchmarkDotNet using a simple example