What is the difference between var and dynamic in C#?
Many people have expressed confusion around the difference between var and dynamic in C#. For both of them, the type is inferred rather than explicitly declared.
dynamic test = 1;
var test2 = 2;
If I hover my mouse over the “var” in the code above, IntelliSense will show me that the compiler has correctly inferred that it is an Int32. If I hover over “dynamic”, it will continue to be typed as “dynamic” since dynamic types aren’t resolved until runtime.
However, var is statically typed, and dynamic is not.
// Can a dynamic change type?
dynamic test = 1;
test = "i'm a string now"; // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
This is one of the key differences between dynamic and var. A var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”. A dynamic variable bypasses all compile-time type checking and resolves everything at runtime.
I’ll comment out the last line in the code above to get the code to compile, and add some code to verify the types of the variables.
// Can a dynamic change type?
dynamic test = 1;
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
test = "i'm a string now"; // compiles and run just fine
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
var test2 = 2;
//test2 = "i'm a string now"; // will give compile error
Console.WriteLine("Var as " + test2.GetType() + ": " + test2);
This produces the following output:
Dynamic as System.Int32: 1
Dynamic as System.String: i'm a string now
Var as System.Int32: 2
Comments
Anonymous
June 16, 2010
"Many people have expressed confusion around the difference between var and dynamic in C#." No offense to those people but they have either been living under a rock or should pick a new career. A much more understandable confusion would be co(ntra)variance.Anonymous
June 17, 2010
Shut up toppe !!! Nobody comes knowing things by birth ! Toppa !!!Anonymous
August 04, 2010
Please shut your nonsense comments.Anonymous
February 11, 2011
Thanks, I was really confused between these two types. What I know that dynamic is dynamically typed and var is statically typed and compiler will not check dynamic types at compile time. But I was confused at why to use dynamic type over var. When I looked these statements // Can a dynamic change type? dynamic test = 1; test = "i'm a string now"; // compiles and runs just fine var test2 = 2; test2 = "i'm a string now"; // will give compile error It sorted all out. Thanks for short and descriptive explanation.Anonymous
March 28, 2011
var must be delcare locally .dynamic can be delcare at class level.Anonymous
March 19, 2012
Why one not use object class instead of dynamic keyword?Anonymous
February 13, 2013
To Anonymous: Using 'object' with a variable calls for boxing that comes with an overhead (cost) and involves switching the memory allocation (to data) between heap (boxing value types) and stack (unboxing ref types).Anonymous
March 07, 2013
Nice one..Check out this blog also for more info about Var and dynamic: senthilvijayalakshmi.blogspot.inAnonymous
March 13, 2013
Good Explanation....! Now ready to face this question in Interview....!Anonymous
April 10, 2013
The comment has been removedAnonymous
June 19, 2013
good.... keep it upAnonymous
July 27, 2014
Dynamic is supported by Dynamic Runtime Library which is a set of functionalities provided over Common Language Runtime.Anonymous
September 02, 2015
Good... easily understand never forget..