The var keyword
The var keyword was introduced in C# 3.0 and variables declared with var are statically typed. Here, the type of variable declared is decided at the compile time. Variables which are declared as var should be initialized at the time of declaration. By looking at the assigned value, the compiler will decide the variable type. As compiler knows the data type of the variable at compile time, errors will be caught at that time only. And Visual Studio 2008 and later versions will show the IntelliSense for var type.
Example
The var keyword was introduced in C# 3.0 and variables declared with var are statically typed. Here, the type of variable declared is decided at the compile time. Variables which are declared as var should be initialized at the time of declaration. By looking at the assigned value, the compiler will decide the variable type. As compiler knows the data type of the variable at compile time, errors will be caught at that time only. And Visual Studio 2008 and later versions will show the IntelliSense for var type.
Example
- var obj "c-sharp corner";
- obj = 10;
The dynamic keyword
The dynamic keyword was introduced in C# 4.0 and variables declared with dynamic were dynamically typed. Here, the type of variable declared is decided at runtime. Variables which are declared as dynamic don't need to initialize the time of declaration. The compiler won't know the variable time at the time of compiling, hence errors can't be caught by the compiler while compiling. IntelliSense is not available since the type of variable will be decided at runtime.
The dynamic keyword was introduced in C# 4.0 and variables declared with dynamic were dynamically typed. Here, the type of variable declared is decided at runtime. Variables which are declared as dynamic don't need to initialize the time of declaration. The compiler won't know the variable time at the time of compiling, hence errors can't be caught by the compiler while compiling. IntelliSense is not available since the type of variable will be decided at runtime.
Example
- dynamic obj = "c-sharp corner";
In the above code, obj will be treated as a string.
- obj = 10;
I hope you have understood the difference between var and dynamic.
Happy learning.

Balmukund GuptaPosted Apr 19, 2022, 10:08 AM
Var is statically typed (compile time), dynamic is dynamically typed (run time)A variable declared as var can only be used locally , dynamic variables can be passed in as params to function (function signature can define a param as dynamic but not var). with dynamic the resolution of the properties happens at runtime and thats not the case with var which means at compile time any variable declared as dynamic can call a method which may or maynot exist and so the compiler would not throw an error. Type casting with var not possible but with dynamic its possible (you can cast an object as dynamic but not as var).
md ameenPosted Apr 14, 2021, 11:37 AM
Good Article sir.
Li LeePosted Nov 16, 2018, 4:30 AM
Var is just syntactic sugar, use it when the name the of type is long or doing a LINQ query with new result
Aditya KasbekarPosted Oct 16, 2018, 9:40 PM
Then why do we even use var keyword when we already know the data type at compile time itself?
Gajendra JangidPosted Mar 28, 2018, 6:53 AM
Nice blog..................
Bhavesh JadavPosted Mar 21, 2018, 6:31 AM
Thanks to clear difference between var and dynamic