Skip is LINQ functionality to filter collection by omitting given number of first elements. Below is an example how to use it. Let’s create a simple collection of strings: var animals = new List<string>() { “dog”, “cat”, “lion”, “duck”, “fish” }; 1 var animals = new List<string>() { “dog”, “cat”, “lion”, “duck”, “fish” }; Now … Continue reading C# – LINQ Skip Example
Author: csharp.academy
C# – LINQ FirstOrDefault Examples
FirstOrDefault is a LINQ functionality to return first element of the collection or default value if requested item does not exist. In case of collection of reference type objects the default is null, whilst in case of value types the default depends on the particular type (e.g. for int it is 0). FirstOrDefault is overloaded … Continue reading C# – LINQ FirstOrDefault Examples
C# – LINQ Select Examples
Select is a LINQ functionality to define a data format of the query results. Each of below examples is presented in C# with both Lambda and Query expression. Let’s create a Person class. public class Person { public string Forename { get; set; } public string Surname { get; set; } public DateTime DateOfBirth { … Continue reading C# – LINQ Select Examples
C# – LINQ Where Examples
Where is a LINQ functionality to filter data in a query with given criteria. Each of below examples is presented in C# with both Lambda and Query expression. 1. Collection of strings – single condition Query collection to get items which start with “b”. var colors = new List<string>() { “red”, “green”, “blue”, “black”, “white” … Continue reading C# – LINQ Where Examples
C# – Custom method of sorting strings
In this article you can find how to create custom method of sorting strings in C#. For the beginning let’s create an example list of strings, which are compass directions in random order. var directions = new List<string>() { “South East”, “North West”, “East”, “South West”, “North”, “West”, “North East”, “South” }; 1 2 3 … Continue reading C# – Custom method of sorting strings
.NET Framework upgrade
How to upgrade .NET Framework? First of all you need to install desired framework version on both development and client machines. Development machine is an environment where you code and build your project. It requires .NET Framework Developer Pack. Client machine is an environment where your application is used. It requires lighter package .NET Framework … Continue reading .NET Framework upgrade
Converting numbers to strings without scientific notation in C#
C# will automatically convert numbers which are of type float, double or decimal and have a lot of precision (lots of numbers after the decimal point) to scientific notation. The means if you have a double which for example contains the value .00009 and attempt to convert it to a string C# will display it as 9E-05. Of course … Continue reading Converting numbers to strings without scientific notation in C#
Discouraging use of the var keyword and ternary if operator
I would always favour typing more code to make it more explicit, more readable and to ensure consistency in style throughout a software system. Minimising the bytes and lines needed to do something shouldn’t take preference over readability. My two pet hates in this regard are the var keyword and ternary (?) if operator. I know var … Continue reading Discouraging use of the var keyword and ternary if operator