“var” and “dynamic” in C#

July 27, 2019

In C#, you must have used the keywords var & dynamic at times while declaring a variable but do you actually know there is a big difference between the two.
Consider the two code snippets:
var value1 = 5;dynamic value2 = 5;
When you run this program, you will see that both value1 & value2 contains an integer value of 5.
Until this point, these looks the same, but here comes the twist.
var value1 = 5;dynamic value2 = 5;value1 = “Hey there”;value2 = “Hey there”;
Take a pause here & try to identify if these are valid statements, then scroll down to solution.
So, what do you think 🤔 is going to happen here. Let’s see.
var value1 = 5;dynamic value2 = 5;value1 = “Hey there”; //gives an invalid cast exceptionvalue2 = “Hey there”; //gives no error
value1 = “Hey there”; statement will give an error while the other statement will execute smoothly. But what is the reason behind this?
To understand this, we need to discuss var & dynamic here.

“var” in C#

var is a statically typed variable. That means, while compiling the program, the compiler identifies what is the type the variable (in our case value1). The type is identified based on the assigned value, 5 in our case which is an integer, hence, the compiler knows that the type of value1 is “int”. var keyword cannot work without an assignment operator.
var x;     //this is an invalid statementvar x = 5; //this is a valid statement & 
           //this code is similar to “int x = 5;”var name = “Kunal” //this is a valid statement & 
                   //this code is similar to string name = “Kunal”;

“dynamic” in C#

dynamic is a dynamically typed variable. That means the compiler need not know about the data type of the variable while compiling. The value is inferred at the runtime & that too dynamically.
Consider the below example,
dynamic x;
/* Notice we do not need any assignment here as the compiler need not to know the type of this variable instead, the compiler just consider it to be a dynamic type */x = 5; 
/* Here x will be considered as an integer as the assigned value (5) is an integer */Console.WriteLine(x); // this statement will print 5x = “Kunal” 
/* Here x will be considered as an string as the assigned value (Kunal) is a string */Console.WriteLine(x); //this statement will print Kunal

Which one to use?

You can use any of the keywords when coding in C#, but which one to use & in which scenario, let’s see.
Since C# is a strongly typed language, it is preferred generally to use the var keyword as it strongly binds the data type of the variable & the datatype cannot be changed at runtime. Using “var” is just a syntactical sugar to the code as it makes it look cleaner.
Consider a class Logger. We can create a new Logger object by writing
Logger logger = new Logger();
or a better approach would be to write,
var logger = new Logger();
both the above statements are exactly same but the lower one looks more decent as it does not repeats the word logger thrice.
dynamic should be used in the case when we are not sure what type of object or value we will be getting in return. Just to accept the value we get, we should use dynamic, although, we may still have to cast it to its concrete type.
To try “var” & “dynamic” in C# Fiddle:

You Might Also Like