Thursday, January 14, 2016

Abstract Class Vs Interface in C#

In today's blog post, I will be discussing a very common interview question i.e. the difference between abstract class and interfaces in C#.

Abstract Class

Any class declared with abstract keyword becomes an abstract class. They are used  to define a contract but when we want to have some default behavior associated with the hierarchy.

Interface

An interface is defined using the interface keyword. Interfaces are used to define a contract and when we do not want to associate any default behavior with the hierarchy of classes.

Abstract Class Vs Interface in C#


So both of them are used to define contract and are important to understand in order for us to use them in our code properly. Let's look at the differences one by one.

Abstract Class can have implementation for some of its members which exists as the default behavior for the hierarchy. The interface can't have implementation for any of it's members. This is the most important difference between the two and guides our choice most of the time when deciding between the two.

The next difference is that interfaces are limited in terms of hierarchy in the sense that an interface  can inherit from interface only. As compared to that, abstract class can inherit from abstract class as well as interface.

Another difference is that abstract class can have fields whereas an interface can not have any fields. Interface can only have methods.

Another really important difference is that a class can inherit from multiple interfaces simultaneously whereas a class cannot inherit from multiple classes at same time.

And lastly, the interface members cannot have access modifiers. Everything is public by default.
Abstract class members can have access modifiers like public, private, etc.

Conclusion

It's one of the most common interview question and still I wonder why so many people get it wrong in their interviews. These differences are important to understand when to use which one. In general, when multiple entities in the system share common behavior then we might use abstract classes. If we just want the entities to follow a contract and not provide any default implementation then we are better with an interface.
For future updates to my weekly blog, please subscribe to my blog via the "Subscribe To Weekly Post" feature at the right and follow me on Twitter. Until then Happy Coding :)


Friday, January 1, 2016

var keyword in C#

In today's blog post we will be discussing the var keyword in C#.

var Keyword

var keyword was introduced in C# 3.0. It is used for implicitly typed local variables. In any statement, if the compiler can decide the data type of the variable with no confusion, we can use the var keyword.


var keyword C#


For example,

var i = 12;       // will work

var j;   // will not work
j = 12;  //implicitly typed variables must be initialized

Since data type of variable j cannot be determined at the time of declaration, it won't compile and we can't use var keyword.

Another thing to note is that with the usage of the var type, the data type is fixed in the initialization statement itself. If we hover the mouse on the var keyword, we can see the data type that compiler is assigning the variable. We should not confuse it thinking that it's a dynamic type. In the first statement itself, the data type is fixed and we can't change the data type dynamically for the variable.

These two statements below are completely equivalent.,

var i = 12;   //implicit declaration

int i = 12;    //explicit declaration

They both produce the exact same IL code. That means that there is no performance effect with the usage of var keyword.

Why use var

Usage of var is more of a syntactic sugar. It makes the code a little shorter and mostly easier to read. For example, if we have some code like this:

ImageThumbnail thumbnail = new ImageThumbnail();


We can write it as:

var thumbnail = new ImageThumbnail();


This helps us remove a small part of the redundant code from the statement and thus reduce code noise.

Another and more important benefit of the var keyword is with anonymous types. So when we want to use an object without going through the hassle of declaring it's class (might make sense if we need to use that object only once), we can use this. For example,

var anonymousObject = new { Id = 1, Name = "Abhi" };

Console.WriteLine(anonymousObject.Name);


So here usage of var prevented me the hassle of creating a new class with 2 properties i.e. Id, Name.

Similarly, another common place where we use anonymous types is while using LINQ:
For example,

var p = from employee in employees
             where employee.Id == 1
             select new { employee.FirstName, employee.LastName };

So here the variable p is IEnumerable<> of anonymous type. Again, usage of var prevented me the hassle of creating a new class which I might not need else where in the code.

When to avoid var

Personally, I prefer to use var when it let's me reduce a few words of redundant code as we discussed earlier in the post. However, I don't like to use it where the data type might be unclear. For example,

var t = SomeMethod();


Here the method name is not telling me what it's returning. So I need to look at the method signature to determine what the data type of my variable is, which makes it less favorable to me. The compiler won't stop you from using it like this, it's a matter of personal preference and readability of the code.

Conclusion

So we saw how var keyword is only a syntactic sugar and does not lead to any performance difference. I use var for anonymous types as well as reducing a few words in the code, wherever possible.

For future updates to my weekly blog, please subscribe to my blog via the "Subscribe To Weekly Post" feature at the right and follow me on Twitter. Until then Happy Coding :)