Thursday, March 21, 2013

Difference between the keywords dynamic, var and object from C# 4.0 onwards

As a seasoned or professional developer in C#.NET, you will find many innovative features in the .NET framework supported to speed up development as well as reduce confusion in code. .NET 3.5 introduced the var keyword, and most of the time many blog posts and articles associated it with LINQ without the need to specify the type explicitly. Then .NET 4.0 introduced a keyword called "dynamic" which made us ask the question why another? As having extensively used System.Object for many interoperability related work, the introduction of these new keywords and it's uses might confuse developers as for why,when and where should these keywords be used etc. Below is an excellent excerpt from an article by Alexandra Rusina (Understanding the Dynamic Keyword in C# 4), which clarifies the differences between the three.

Dynamic, Object or Var?

So what’s the real difference between dynamic, object and var, and when should you use them? Here are short definitions of each keyword and some examples.

The object –keyword represents the System.Object type, which is the root type in the C# class hierarchy. This keyword is often used when there’s no way to identify the object type at compile time, which often happens in various interoperability scenarios.

You need to use explicit casts to convert a variable declared as object to a specific type:

object objExample = 10;

Console.WriteLine(objExample.GetType());

This obviously prints System.Int32. However, the static type is System.Object, 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";

The var keyword, since C# 3.0, is used for implicitly typed local variables and for anonymous types. This keyword is often used with LINQ. When a variable is declared by using the var keyword, the variable’s type is inferred from the initialization string at compile time. The type of the variable can’t be changed at run time. If the compiler can’t infer the type, it produces a compilation error:

var varExample = 10;

Console.WriteLine(varExample.GetType());

This prints System.Int32, and it’s the same as the static type.

In the following example, no cast is required because varExample’s static typed is System.Int32:

varExample = varExample + 10;

This line doesn’t compile because you can only assign integers to varExample:

varExample = "test";

The dynamic keyword, introduced in C# 4, makes certain scenarios that traditionally relied on the object keyword easier to write and maintain. In fact, 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:

dynamic dynamicExample = 10;

Console.WriteLine(dynamicExample.GetType());

This prints System.Int32.

In the following line, no cast is required, because the type is identified at run time only:

dynamicExample = dynamicExample + 10;

You can assign values of different types to dynamicExample:

dynamicExample = "test";

There’s a detailed blog post about differences between the object and dynamic keywords on the C# FAQ blog (bit.ly/c95hpl).

What sometimes causes confusion is that all of these keywords can be used together—they’re not mutually exclusive. For example, let’s take a look at this code:

dynamic dynamicObject = new Object();

var anotherObject = dynamicObject;

What’s the type of anotherObject? The answer is: dynamic. Remember that dynamic is in fact a static type in the C# type system, so the compiler infers this type for the anotherObject. It’s important to understand that the var keyword is just an instruction for the compiler to infer the type from the variable’s initialization expression; var is not a type.