.Net Interview Questions – Difference between var and dynamic

This will be the first entry about common questions in a .net interview. The main idea is explain clearly the most common concepts every .net developer should have. I know most of the questions in an interview make no sense but be aware when an Interviewer is asking is because he is expecting an specific answer from our side.

Difference between var and dynamic

For those are not familiar with those keywords, var and dynamics allow us to do not define the data type when we are declaring a variable. In this way a variable declaration looks like this.

var x = "string";
dynamic y = "string";

If we make a comparison between those variables the result will be that both variables are strings and both have the word “string”.

Both give us the result of the same object type, then what there are two keywords that make the same for? They appear to make the same but they do  not.  How is that possible? Well it is easy. The main difference  between those keywords is when they  join the variable with the data type and in my opinion this is the biggest advantage of dynamics over var.

When we declare a variable using the keyword var we must initialize this variable right away. Why? Well the data type is going to be joined to the variable while we are coding. It means when we initialize the variable this will be joined to the data type  we are using to initialize it. Even if you have the impression that you are not defining a data type you are using one so your code is still strongly typed and you wont be able to store in that variable something else that you stored during the initialization.

In the other hand when we declare a variable using dynamic it is not necessary initialize the variable to make it works. Why? Because dynamic is going to join the data type during the execution using reflection.  As you can imagine one of the strongest point of dynamic is become the variable a weakly typed. This allow us to make the following usage.

dynamic y = "string";
Console.WriteLine("It is: {0}", y);
y = 1;
Console.WriteLine("It is: {0}", y);

The biggest advantage of dynamic is the flexibility as we could see before anyway a disadvantage, as every weakly typed language is that we are more plausible to make a mistake and do not know what is store in this variable. Because is using reflection we wont know the issue until running time.

The advantage of var is that allow us to reduce the space during the initialization or we might do not know which is the data type of a LINQ execution. In those cases var allow us to declare quickly a variable with out many problems.

In my opinion what the interviewer is looking with this question is to know how much we know of the .net framework or if we just repeat because some one tells you you have to do it in that way.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s