Check If IEnumerable<> or List<> Type is Empty / Null

There are three ways to check whether IEnumerable<> or List<> type is empty / null or not. == NULL .Any() .Count() In any case, you always want to combine “== null” and, either “.Any()” or “.Count()”, with OR (“||”) operator. In addition, you want to put “== null” before the other condition. The reason for this is, “if” condition goes from left to right. So, if the list object is null, it will not go to the second check,…

Read More

Visual C# Code Snippets

By default the following code snippets are included in Visual Studio. Name (or shortcut) Description Valid locations to insert snippet #if Creates a #if directive and a #endif directive. Anywhere. #region Creates a #region directive and a #endregion directive. Anywhere. ~ Creates a destructor for the containing class. Inside a class. attribute Creates a declaration for a class that derives from Attribute. Inside a namespace (including the global namespace), a class, or a struct. checked Creates a checked block. Inside a method, an indexer, a property accessor, or an event…

Read More

Foreach C#

A program iterates over a collection. The index of each element is not needed. Only the elements are needed. With foreach we access just the elements. A foreach-loop, this is the easiest, least error-prone loop. It is preferred in many program contexts. Here are some samples: using System; class Program { static void Main() { string[] pets = { “dog”, “cat”, “bird” }; // … Loop with the foreach keyword. foreach (string value in pets) { Console.WriteLine(value); } }…

Read More