tags:

views:

950

answers:

4

Hello,

I had read a ton of articles about that new keyword that will ship with C# v4,but I couldn't make out the difference between a "dynamic" and "var".

This article made me think about it,but I still can't see any difference.

Is it that you can use "var" only as a local variable,but dynamic as both local and global?

I'm sorry for my ignorance,but could you show some code without dynamic keyword and then show the same code with dynamic keyword?

+6  A: 

Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed. This capability was added to the CLR in order to support dynamic languages like Ruby and Python.

I should add that this means that dynamic declarations are resolved at run-time, var declarations are resolved at compile-time.

HVS
+1 for conciseness
+1 knowing why it was added, the mystery revealed
GenEric35
+31  A: 

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 know anything 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.

Marc Gravell
If I only could give another +1 for the FlibbleBananaSnowball property ... :-)
Joey
An interesting question would be if there are dynamic ancestors of statically declared classes. Example: class X { public int Y {get;set;} }dynamic(X) s = GetSpecialX();Calling string test = s.Y; would generate a compiler error because the compiler knows about Y but string test2 = s.Z would compile fine and be checked at run-time. I could think of much value of such half-dynamic classes!
rstevens
@rstevens - IIRC, you can add dynamic behaviour via an interface (although there is no direct language support for implementing dynamic types in C# - only consuming them), so this isn't unrealistic... oh the fun we could have ;-p
Marc Gravell
+1  A: 

var is just a shorthand for a normal type declaration, where you let the compiler guess the correct type.

dynamic is a new (static) type, where all checks are done at runtime, not by the compiler.

gimel
+1  A: 

The type of a variable declared with var is determined by the compiler, it is a shortcut to specifying the type's name, nothing more.

However dynamic is determined at runtime, the compiler has no idea of the actual type, and all method/field/property accesses with that variable will be worked out at runtime.

Richard