Friday, 25 November 2011

Difference between Var , Dynamic , Object Keyword

Var is type safe ie when we click on the dot(.) it will display the public members of the type which the var is currently holding.But dynamic on the other hand will not be able to display the list.There will be no compilation error on the dynamic keyword even if we use some junk characters as its member methods or properties.Its just like the old style programming where there is no type safety.At run time the method or property may or may not be there. 
Internally how dynamic is handled is just a reflection code.It checks for the member through reflection and it invokes the member if exists.Else throws exception.If you want to see the reflected code of dynamic keyword just use the reflector

var is static typed - the compiler and runtime know the type - they just save you some typing... the following are 100% identical:
var s = "abc";
Console.WriteLine(s.Length);
and
string s = "abc";
Console.WriteLine(s.Length);
All that happened was that the compiler figured out that s must be a string (from the initializer). In both cases, it knows (in the IL) that s.Length means the (instance) string.Length property.
dynamic is a very different beast; it is most similar to object, but with dynamic dispatch:
dynamic s = "abc";
Console.WriteLine(s.Length);
Here, s is typed as dynamic. It doesn't know about string.Length, because it doesn't knowanything about s at compile time. For example, the following would compile (but not run) too:
dynamic s = "abc";
Console.WriteLine(s.FlibbleBananaSnowball);
At runtime (only), it would check for the FlibbleBananaSnowball property - fail to find it, and explode in a shower of sparks.
With dynamic, properties / methods / operators / etc are resolved at runtime, based on the actual object. Very handy for talking to COM (which can have runtime-only properties), the DLR, or other dynamic systems, like javascript.

No comments:

Post a Comment