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 (13)

  1. josheinstein says:

    "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.

  2. Garry says:

    Shut up toppe !!!

    Nobody comes knowing things by birth !

    Toppa !!!

  3. kail says:

    Please shut your nonsense comments.  

  4. Harish Moolchandani says:

    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.

  5. There is one more difference says:

    var must be delcare locally .dynamic can be delcare at class level.  

  6. Anonymous says:

    Why one not use object class instead of dynamic keyword?

  7. Vaibhav Singh says:

    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).

  8. Senthil says:

    Nice one..Check out this blog also for more info about Var and dynamic: senthilvijayalakshmi.blogspot.in

  9. U.D says:

    Good Explanation….! Now ready to face this question in Interview….!

  10. Cool says:

    Cherry cherry

    boom boom

    GaGa

    Boy, we've had a real' good time

    And I wish you the best on your way

    Eh eh

    I didn't mean to hurt you

    I never thought we'd fall out of place

    Eh eh, hey ey

    I had something that I love long

    But my friends keep telling me that something's wrong

    Then I met someone

    And eh, there's nothing else I can say

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    I wish you'd never looked at me that way

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    Not that I don't care about you

    Just that things got so compliquées

    Eh eh

    I met somebody cute and funny

    got each other and that's funny

    Eh eh, hey ey

    I had something that I love long

    But my friends keep telling me that something's wrong

    Then I met someone

    And eh, there's nothing else I can say

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    I wish you'd never looked at me that way

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    (Eh eh, eh eh, eh eh…)

    I had something that I love long

    But my friends keep telling me that something's wrong

    Then I met someone

    And eh, there's nothing else I can say

    Eh eh, eh eh

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    I wish you'd never looked at me that way

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    I wish you'd never looked at me that way

    Eh eh, eh eh

    There's nothing else I can say

    Eh eh, eh eh

    (Cherry cherry, boom boom)

    Eh eh, hey ey

    Oh yeah

    All I can say is eh eh

  11. VInoth says:

    good…. keep it up

  12. Vinod says:

    Dynamic is supported by Dynamic Runtime Library which is a set of functionalities provided over Common Language Runtime.

  13. raghupathi says:

    Good… easily understand never forget..