Programming languages can normally be considered to be either statically typed or dynamically typed. A static (not to be confused with the static keyword, used for classes) typed language validates the syntax or checks for any errors during the compilation of the code. On the other hand, dynamically typed languages validate the syntax or check for errors only at run time. For example, C# and Java are a static type and JavaScript is a dynamically typed language.

C# was previously considered to be a statically typed language since all the code written was validated at the compile time itself. But, with the introduction of the dynamic keyword in C# 4.0, it became a dynamically typed language. The two concepts of static and dynamic types in C# can be illustrated with the use of the two keywords named var and dynamic. Let's discuss some basic differences between these two, based on some of their characteristics.

When were they introduced

Type inference of variables

Initialization of variables

Changing the type of value assigned

Intellisense help

Restrictions on the usage

var vs dynamic vs object

If we closely observe the dynamic type, it is performing pretty much the same task that the object type (which is the base type of all other types) does. Then what is the difference between an object type and a var then? Also, why do we need a var when we have the object type? Let's discuss these points by doing some comparisons.

  1. When using an object type, the compiler provides only generic information, functionality, or functions related to the type it holds, until it is being typecast into its actual type. This means that even if we have stored integer or string values in an object type, we will get their related functions, only if we convert the object type into its actual type. See the code below.
    Object type
  2. Here, we only have generic properties and functions available for the type of value stored in it, that we will have, even if we store an integer or other type of value in it. Now, let's convert it to its actual type, string.
    Generic properties
  3. Now after explicit conversion, we have the properties and functions specific to the string type. In case we use the var type to store the same value, we would not be required to do the explicit conversion, since the compiler already infers its type from the value initialized and we will have its functions and properties.
  4. Based on the preceding point, object types increase the overhead of boxing and unboxing, before we can use the actual values stored in it or use any function related to it. But this is not the case with var, since we already know its type at the time of use. For example, in the code above, we get to use the functions related to a string only after we convert the object into a string. So this results in un-boxing. On the other hand, for dynamic types, we only need to know that the function or property we are using actually exists for the type of value being stored in it.
    Functions
  5. For example, in the code above, if we know that the dynamic variable will be storing a string type, then we can use the Length type property, even if it is not available in the IntelliSense help.
  6. Next, we need to be careful when casting or converting the values, when using the dynamic or object type variables. Any incorrect casting can result in runtime errors. On the other hand, a var type will give a compile time error for an incorrect conversion. So a run time error is not possible.
    Run time error
  7. Here, the object type is stored as a string value, but we are trying to cast it into an integer type. So it will throw a run time error.
    Integer type
  8. Similarly, we are storing back the string value, in an integer type. So it would again result in an error.
  9. In terms of interoperability with various frameworks, earlier we required an object type to be used to get the underlying object type and then use reflection to access the related methods or functions. But with the introduction of the dynamic keyword, we only need to know that a function or property exists on the underlying object, and for the rest, we simply need to make a call to these properties or functions.
  10. As per MSDN, "As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.".

From these points, we cannot conclude which specific type we should use. It all depends on the requirements of whether we should go for a var dynamic or object type.

So this was all about the basic differences between var and dynamic keywords. I hope you enjoyed reading it.