Var vs Dynamic Keywords in C#



In this post I'm going to explain about the Var vs Dynamic Keywords in C#.


1. When were they introduced
Var was introduced in C# 3.0
Dynamic was introduced in C# 4.0
2. Type inference of variables
Var is a statically typed variable. It results in a strongly typed variable, in other words the data type of these variables are inferred at compile time. This is done based on the type of value that these variables are initialized with.
Dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time in contrast to var type.
3. Initialization of variables
Var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
Dynamic type variables need not be initialized when declared.
4. Changing type of value assigned
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, on assigning the integer value, it will be treated as an integer type thereafter. So thereafter no other type of value cannot be assigned. For example, the following code will give a compile time error:
    static void Main(string[] args)
        // Declare as a int.
        var varVariable = 007;

        // Declare as a string.
        varVariable = "007";

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, at run time, the value of the variable is first inferred as Int32 and when its value is changed, it is inferred to be a string type.
    static void Main(string[] args)
        // Declare as a int.
        dynamic dynamicVariable = 007;

        // Declare as a string.
        dynamicVariable = "007";

5. Intellisense help
Intellisense help is available for the var type of variables. This is because, its type is inferred by the compiler from the type of value it is assigned and as a result, the compiler has all the information related to the type. So we have the intellisense help available. See the code below. We have a var variable initialized with a string value. So its type is known to the compiler and we have the appropriate intellisense help available for it.

Intellisense help is not available for dynamic type of variables since their type is unknown until run time. So intellisense help is not available. Even if you are informed by the compiler as "This operation will be resolved at run-time". See the code below :
6. Restrictions on the usage
Dynamic variables can be used to create properties and return values from a function.
Var variables cannot be used for property or return values from a function. They can only be used as local variable in a function.


Guys hope I explain it peoperly.

You Might Also Like