Saturday, January 17, 2015

Var vs dynamic keywords in C#

Programming languages can be normally considered to be either statically typed or dynamically typed. A static (Not to be mixed with static keyword, used for classes) typed language validates the syntax or checks for any error during the compilation of the code. On the other hand, dynamically typed languages validate the syntax or checks for any error, only at the run time. For ex, C# & JAVA are a static type and javascript is dynamic type language.
C# was earlier considered as a statically typed language, as 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 dynamic typed language also. The two concepts of static and dynamic types in C# can be illustrated with the use of two keywords named var and dynamic.  Let's discuss about some basic differences between these two, based on some of their characteristics.
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 type variable. It results in a strongly typed variable i.e. the data type of these variables are inferred at compile time. This is done based on the type of value, with which these variables are initialized.
  • 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 run in compile time error as : 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 allows to change the type of value assigned, which is once assigned to it. This means, if we assign an integer value to a var, we cannot assign string value to it. This is because, on assigning the integer value, it will be treated as of integer type thereafter. So further any other type of value cannot be assigned. For ex, following code will give a compile time error

  • dynamic allows to change the type of value, assigned to it initially. In the above code, 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 to the variable is first inferred as Int32 and when its value is changed, it is inferred as string type.

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, compiler has all the information related 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 as their type is unknown till run time. So intellisense help is not available. Even you will be informed by 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 value from a function. They can be only used as local variables in a function.
var vs dynamic vs object ?
If we closely observe dynamic type, it is performing pretty much the same task which the object type (which is the base type of all other types) does. Then what is the difference between object type and var then ? Also, why we need var when we have the object type. Let's discuss these points by doing some comparisons.
  1. While using object type, compiler provides only generic information, functionality or functions related to the type it holds, until it is being type cast into its actual type. This means, even if we have stored any 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 :

Here, we only have generic properties and functions available for the type of value stored in it, which we will have, even if we store any integer or other type of value in it. Now, let's convert it to its actual type i.e. string.

Now after explicit conversion, we have the properties & 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, as compiler already infers its type from the value initialized and we will have its functions and properties.
2.  Based on the above point, object types increase the overhead of boxing and un-boxing, 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, as we already know its type at the time of use. For ex, in above code, we get to use the functions related to string only after we convert the object into 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.

For example, in above code, if we know that dynamic variable will be storing string type, then we can use Length type property, even if it is not available in the intellisense help.
3. Next, we need to be careful while casting or converting the values, when using the dynamic or object type variables. Any wrong casting can result in run time errors. On the other hand, var type will give a compile time error with wrong conversion. So any run time error is not possible.

Here, object type stored string value, but we are trying to cast into integer type. So will throw run time error.

Similarly, we are storing back the string value, into integer type. So it would again result in error.
4.  In terms of interoperability with different frameworks, earlier we required 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 dynamic keyword, we only need to know that a function or property exists on the underlying object and rest we simply need to make a call to these properties or functions.
5. 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 requirements whether we should go for var or dynamic or object type.
So this was all about the basic differences between var and dynamic keywords. Hope you enjoyed reading it...!!!

No comments:

Post a Comment