Category Archives: C#

What are events in C#?

Events Events is a way for a class to provide notifications to clients of that class when some interesting things happens to an object. Events are declared using delegates. Events can only be invoked from within the class that declared them. Derived classes can not directly invoke events declared within the base class. To call Continue Reading…

When to use var instead of type in C#.

Example 1 Here it is an optional to declare i as var type. Because we know the value is integer type. Example 2 it is also an optional to use var instead of string. Because we know all elements are string. Example 3 Use var when we have to deal with anonymous type data.

Difference between Abstraction and Encapsulation.

Abstraction Abstraction means hiding implementation. Abstraction can be achieved by interfaces or abstract classes. Abstraction focuses on the outside view of an object (i.e. the interface) TV Remote is the real world example of abstraction where we know little bit how to operate it. but does not know how they are implemented internally. Encapsulation Encapsulation Continue Reading…

Why multiple inheritance of classes is not supported in C#?

Multiple inheritance of classes is not supported in C# because of the diamond problem. Diamond Problem: If in a scenario there is a Class A have a Print method. And another classes B and C is overriding Class A Print method with single inheritance concept. Now we have a class D which inherit both these classes B Continue Reading…

Difference between Convert.ToString() and .ToString() method in C#.

Convert.ToString() method can handle null value and will not throw exception. Example: .ToString() method will throw exception (Object reference not set to an instance of an object.) while converting null value to string. Example: Exception : Object reference not set to an instance of an object.

Passing value to base class constructor c#.

“base” keyword is used to call and pass the value to the base class parameterised constructor in c#. Base class constructor is called before the child class constructor.

When to use interface and abstract class in C#.

Abstract classes have some implemented functionality so you can use the same functionality among all your implementations. Its the best example of re-usability of code. While Interfaces don’t provide any implementation of code. If you are thinking about multiple version of your component then create abstract class. Abstract classes provide a simple and easy way Continue Reading…

What is difference between overloading and overriding in C#?

Difference between overloading and overriding. Overloading : It means methods are declared with same name but different parameters (signatures). It is also called compile time and early binding. Example :- Overriding : It means methods are declared with same name and same parameters (signatures). it is also called run time and late binding. Method overriding is only possible Continue Reading…

What is Static Constructor ?

A static constructor is used to initialize any static members, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. Example Note : A static constructor does not take access modifiers or any parameters. A static constructor Continue Reading…