Extension methods in C#

Extension methods are a new feature in C# 3.0. An extension method enables us to add methods to existing types without creating a new derived type, recompiling, or modify the original types. We can say that it extends the functionality of an existing type in .NET. An extension method is a static method to the […]

Read More

Difference between Value Type and Reference Type

Value Type Reference Type Data stored in stack memory Data stored in heap memory When passed as value type new copy is created and passed so changes to variable does not get reflected back. When passed as Reference type then reference of that variable is passed so changes to variable does get reflected back. Value […]

Read More

Event vs Delegate

Event and Delegate are interrelated. In this post, we will learn how they are different each other. An Event declaration adds a layer of abstraction and protection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the […]

Read More

Difference between Singleton and Static class

A singleton classes allowed to create a only single instance or particular class. That instance can be treated as normal object. You can pass that object to a method as parameter or you can call the class method with that Singleton object. While static class can have only static methods and you can not pass […]

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

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

Difference between Lazy Loading and Eager Loading

Lazy/Deferred Loading In lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading. With Lazy Loading, we only retrieve just the amount of data, which we need in a single query. When we need more data related to the initial data, […]

Read More

Why we need Multi-threading in our project

Multi-threading is running the multiple threads simultaneously. We can do multiple tasks simultaneously with the help of Multi-threading. For e.g. saving the details of user to a file while at the same time retrieving something from a web service. Threads are much lightweight than process. They don’t get their own resources. They used the resources […]

Read More

Static Class in C#

Static Class:- We can create static class or member by using static keyword. Static class or member can be used when object of class or member not change every time. Static class or member are loaded at time of namespace are loaded by CLR. We cannot create instance of static class because static class member […]

Read More

Anonymous method in C#

Anonymous is a type that doesn’t have any name. Anonymous type allows you to create new type by using the new keyword and object initializer syntax without defining its class. The var is used to hold the reference of anonymous types. The Type of anonymous is generated by c# compiler that is accessible within the […]

Read More

Difference between Finalize and Dispose

A finalizer (aka destructor) is part of garbage collection (GC) – it is indeterminate when (or even if) this happens, as GC mainly happens as a result of memory pressure (i.e. need more space). Finalizers are usually only used for cleaning up unmanaged resources, since managed resources will have their own collection/disposal. Hence IDisposable is […]

Read More

Understand Inheritance and Polymorphism with example

In this post, we will understand Inheritance and Polymorphism concept of OOP’S practically. In most of the interviews, interviewers ask these inheritance questions and try to confuse you but after reading this post you will easily understand and clear all your confusion. When you derive a class from a base class, the derived class will inherit […]

Read More