LINQ Single vs SingleOrDefault vs First vs FirstOrDefault

Single(), SingleOrDefault(), First() and FirstOrDefault() are extension methods of the Enumerable class. Many people get confused about the difference between Single, SingleOrDefault, First, and FirstOrDefault methods in Linq. Let’s understand the difference between all these methods as below: Single() SingleOrDefault() First() FirstOrDefault() Description Returns a single, specific element of a sequence Returns a single, specific […]

Read More

Constructor Chaining in C#

Constructor chaining is an approach where a constructor calls another constructor in the same class or base class. It is very useful when we have a class that defines multiple constructors. Before knowing how to use constructor chaining in C#. Let’s first see why we need constructor chaining. Example: We have a class called “Employee” and […]

Read More

Nullable type or double question mark in C#

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand. Example:- int? x = null; // Set y to the value of x if x is NOT null; otherwise, set y to -1. int y = x ?? -1;

Read More

ROW NUMBER, RANK and DENSE RANK in SQL

In this post, We will learn the difference between Row_Number, Rank, Dense_Rank and how to use with example. ROW_NUMBER, RANK and DENSE_RANK functions are used to calculate RowID for the result set which is returned from a query but in a slightly different way. Firstly, We will create a table and insert some record for […]

Read More

Difference between throw and throw ex

We can say “throw” maintains the full hierarchy in the stack trace and gives complete information about the exception occurred in the code. Whereas “throw ex” pretends that exceptions occurred on the line where “throw ex” was written and removes all the hierarchy above the method containing the “throw ex” expression.

Read More

Difference between IEnumerator and IEnumerable

IEnumerable An enumerable class implements the IEnumerable interface. The IEnumerable interface has only one method GetEnumerator, which returns an IEnumerator interface. public IEnumerator GetEnumerator(); Example:- class Program { static void Main() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.Add(5); List<int>.Enumerator e = list.GetEnumerator(); Write(e); } static void Write(IEnumerator<int> e) { while (e.MoveNext()) […]

Read More

How to start a Thread in C#

We have to use the Thread class provided by System.Threading namespace. In the constructor of the class, we have to pass the method name which we want to run in separate thread. After than we have to call the start method of Thread class. Example:- using System; using System.Threading; namespace ConsoleApplication1 { class Program { […]

Read More