var vs dynamic vs object

C# provides three types object, var and dynamic to store data of any type. In this article, I have tried to explain difference between these 3 and scenarios to use them.

  1. History
    object was introduced in C# 1.0
    var was introduced in C# 3.0
    dynamic was introduced in C# 4.0
  2. Type Safety
    object is partially type safe. Compiler has little information about this type at compile time.
    var is type safe i.e. statically typed. It means type of variable is decided by compiler at compile time and it has all information about the stored value, so that it doesn't cause any issue at run-time.
    dynamic is not type safe i.e. dynamically typed. It means type of variable is not decided by compiler at compile time and it does not have any information about the type of variable.
  3. Initialization

    object type variable is not required to initialize at the time of declaration.

    object string1 = “This is test Variable 1”; // Works Fine and compiles
    object string2;			        // Works Fine and compiles
    string2 = “This is test Variable 1”;

    var type variable is required to initialize at the time of declaration.

    var string1 = “This is test Variable 1”;  // Works fine and compiles
    var string2;				// Does not work fine and give compile time error
    string2 = “This is test Variable 1”;

    dynamic type variable is not required to initialize at the time of declaration.

    dynamic string1 = “This is test Variable 1”; // Works Fine and compiles
    dynamic string2;			  // Works Fine and compiles
    string2 = “This is test Variable 1”;
  4. Errors on compile time vs run time
    object - Since the compiler does not know about the type at the compile time so errors are not caught at compile time. It causes problem at run time if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
    var - Since the compiler knows about the type at the compile time so errors are caught at compile time and it does not cause any problem at run time.
    dynamic - Since the compiler does not know about the type at the compile time so errors are not caught at compile time. It causes problem at run time if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
  5. Intellisense Help
    object - Visual Studio does not show intellisense help since the type of variable is not known to compiler. It will show methods of object class only in intellisense.
    var - Visual Studio shows intellisense help since the type of variable is known to compiler.
    dynamic - Visual Studio does not show intellisense help since the type of variable is not known to compiler.
  6. Changing type of value assigned

    object allows the type of value to change after it is assigned to initially. In the code above, if we use object instead of var, it will not only compile, but will also work at run-time. This is because, compiler creates the type for test1 as System.Int32 and then recreates the type as string when the value “This is a string” was assigned to it.

    object test1 = 2 
    test1 = “This is a string”;	// Works fine

    var does not allow the type of value assigned to be changed after it is assigned to. This means that if we assign an integer value to a var then we cannot assign a string value to it. This is because, compiler has already decided that the type of that variable is System.Int32 when the value was assigned to it. Now assigning a string value to it violates the type safety.

    var test1 = 2 
    test1 = “This is a string”;	// Compile time error due to type safety violation

    dynamic allows the type of value to change after it is assigned to initially. In the code above, if we use dynamic instead of var, it will not only compile, but will also work at run-time. This is because, compiler creates the type for test1 as System.Int32 and then recreates the type as string when the value “This is a string” was assigned to it.

    dynamic test1 = 2 
    test1 = “This is a string”;	// Works fine
  7. Usage
    object type can be used for property, passed as method argument and method also can return object type.
    var type cannot be used for property, cannot be passed as method argument and method cannot return object type. var can only be used as local variable in a function.
    dynamic type can be used for property, passed as method argument and method also can return dynamic type.
  8. Casting
    object variable need to cast to original type to use it and to perform desired operations because compiler does not has any information about its type. As result, it increases the overhead of boxing and un-boxing.
    var variable need not to cast to original type to use it and to perform desired operations because compiler has all information about its type.
    dynamic variable need not to cast to original type to use it and to perform desired operations but you need to know the properties and methods related to stored type.
  9. Real Time Usability Scenario
    object is useful when we don’t have more information about the data type.
    var is useful when we don’t know actual type i.e. type is anonymous. It is useful when getting result from linq queries.
    dynamic is useful when we need to write code for reflection or dynamic languages (like IronRuby and IronPython) or with the COM objects, because we need to write less code. It is also useful when we are interoperating with Office Automation API’s. This saves us from casting everything from the object. We only need to know that a function or property exists on the underlying object and the rest we simply need to make a call to these properties or functions.

    So, if you often use the object keyword and have to perform a lot of casting and/or use reflection to call methods and properties of objects, you probably should take a look at the dynamic keyword. In some cases it’s more convenient than object and with less code to write.