Difference between dynamic, object and var keyword.


Here we are going to see the Difference between dynamic, object and var keyword. Below is the code snippet for using dynamic, object and var keywords in dot net framework in c#. dynamic keyword is a new feature in c# 4.0. we are going to see the significance of the following keywords in C# and the differences in their behavior compared to other keywords.

In this code snippet we are going to see the significance of the following keywords in C# and the differences in their behavior compared to other keywords. We will also see how it works with other languages and other features of .Net framework.

1. object
2. var
3. dynamic

dynamic keyword is a new feature in c# 4.0 we cannot switch dynamic features on or off. Dynamic keyword tells the compiler to turn off compile-time checking. It is possible to assign objects of different types to a variable declared as dynamic. The code compiles and it determines the type of object at runtime Lets take an example:


dynamic dyn = "testing";
Console.WriteLine(dyn.GetType());//output is System.String.

dyn = 57;
Console.WriteLine(dyn.GetType());//output is System.Int32.

dyn = "dynamic";
dyn = dyn++;

Above line compiles as well but throws an error at runtime because the compiler doesnot know the runtime type of the object at compile time and therefore cannot tell that the increment operation is not supported in this case.
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '++' cannot be applied to operand of type 'string'.
one point to observe is that the dynamic declared variables doesn't have intelliSense feature because the C# compiler doesn't know the type of dynamic variable, it can't enumerate its properties and methods.
When there is no way to identify object type at runtime then we go for Object type. Object keyword represents the System.Object type. it is the root type in the c# class hierarchy. It is mostly used in interoperability scenarios.


object objExample = 10;
Console.WriteLine(objExample.GetType());//outputs System.Int32
objExample++;//compile time error: Operator '++' cannot be applied to operand of type 'object'


The above line gives an error as system.Object is a static type, so you need an explicit cast here:

objExample = (int)objExample + 10;
You can assign values of different types because they all inherit from System.Object:
objExample = "test";


var keyword:
The var keyword is used for implicitly typed local variables and for anonymous types. When a variable is declared by using the var keyword, the variable's type is inferred from the initialization string at compile time. we cannot change the type of the variable at run time. If the compiler can't infer the type, it produces a compilation error:

var varExample = 10;
Console.WriteLine(varExample.GetType());//outputs System.Int32, and it is the same as the static type.
In the below example, no cast is required because varExample is static typed is System.Int32:
varExample = varExample + 10;

Below line doesn't compile because you can only assign integers to varExample as it is already initialized to int value while declaring.
varExample = "test"; //Cannot implicitly convert type 'string' to 'int'

the dynamic type uses the System.Object type under the hood, but unlike object it doesn't require explicit cast operations at compile time, because it identifies the type at run time only:


Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

Author: Pritom Nandy12 May 2012 Member Level: Gold   Points : 0

Hi,
It was a good piece of information.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: