dynamic vs var in C# [duplicate]

Solution 1:

var means the static type is inferred - in your case it's exactly equivalent to

A a1 = new A();

All the binding is still done entirely statically. If you look at the generated code, it will be exactly the same as with the above declaration.

dynamic means that all any expression using a2 is bound at execution time rather than at compile-time, so it can behave dynamically. The compiler won't check whether the Foo method exists - the behaviour is determined at execution time. Indeed, if the object implements IDynamicMetaObjectProvider it could decide what to do with the call at execution time, responding to any method call (or other kind of use) - in other words, there doesn't have to be a "real" method called Foo at all.

If you look at the generated code in the dynamic situation, you'll find all kinds of weird and wonderful stuff going on to do with call sites and binders.

Solution 2:

var is type safe, in that it uses type inference. Writing var a = new A(); is a shorthand for A a = new A();. A variable which is declared dynamic is NOT type safe and the compiler does nothing to check that the methods you call on it exist.

Solution 3:

In the var case, A() has to have a .Foo(int) method on it during compilation.

In the dynamic case, it doesn't.