Though the usage of dynamic keyword seems quite interesting; but it could become very hard to catch errors. For instance, a new developer who doesn’t know much about it adds a property that was not required; the compiler won’t show an error – only, probably runtime will. This means, typos won’t be picked up during compile time because you can just declare about anything anywhere.

Plus, this code gives an impression that it type-safe, which it clearly, is not.

Similar to, that you can’t use C# v2.0 features without at least VS 2005 and you can’t use C# v3.0 features without VS 2008; it is that you cannot use EF v4.0 in VS2008.

Still a ‘why’? Follow on…

Because EF v4.0 requires .NET v4.0(that is, 4.0.30319.1 to be exact – stable release out in the market, approximately 38 days ago); which means EF v4.0 is actually a part of .NET v4.0 – And, 3.5 uses CLR v2.0 while 4.0 uses CLR v4.0.

Among its many new improvements, Visual Studio 2010 introduces the much-awaited Entity Framework 4.0 and WCF Data Services 4.0 (formerly ADO.NET Data Services), which together simplify how you model, consume and produce data.

So to conclude: This means,

.NET v1.0 was used with VS2002 having C# v1.0

.NET v1.1 – VS2003

.NET v2.0 – VS2005 – C# v2.0

.NET v3.0 – Useable in VS2005

.NET v3.5 – Visual Studio 2008 having C# v3.0

.NET v4.0 – Visual Studio 2010 with C# v4.0

And according to Jon Skeet on SO, C# v5.0 contains major new speculated features such as Meta Programming.

Like this:

There is a new type, a contextual keyword, called dynamic in C# v4.0; getting to much of a popularity among advanced programming.It is used to call operations of dynamic languages (DLR).

To put it simply, binding is the association of a symbol within a program with an address in memory. The term “binding”, in this case refers to the process of resolving types, members, and operations.

Static binding happens at compile time, where a compiler identifies, resolves, and throws exception if necessary, at compile time.

Dynamic binding refers to identification, resolution, and throwing of exception if any, during runtime.

Dynamic binding in useful in cases where you know that a certain member, function, or operation would be available at run time. But compiler doesn’t know about that; the compiler would not throw any exceptions as well because it would just “trust you”, because you used dynamic keyword.

“A dynamic type tells the compiler to relax. We expect the runtime type of d to havea Quack method. We just can’t prove it statically.” – (C# in a Nutshell Chap-4 Pg161)

dynamic dynTest = new Test();dynTest.AnotherTest(1);

dynamic means that all expressions using dynTest is bound at execution time, rather than at compile-time, and thus makes it behave dynamically.

dynamic versus var keyword

A lot of times dynamic is confused with the var keyword; and to prove that statement, following is such an example that creates quite a confusion.

//CODE 1var varTest = new Test();varTest.AnotherTest(1);

//CODE 2dynamic dynTest = new Test();dynTest.AnotherTest(1);

Here, in CODE 1, var means the static type is inferred – in this case it’s same as

Test varTest = new Test();

var is static typed – the compiler actually knows the type All the binding is still done entirely statically.

dynamic, in CODE 2, means that all expressions using dynTest is bound at execution time, rather than at compile-time, and thus makes it behave dynamically. Compiler won’t check whether the AnotherTest() method exists – the behavior is determined at execution/run time.

Note that, somewhat on a tangent, if the object implements IDynamicMetaObjectProviderit could decide what to do with the call at execution time, responding to any method call – in other words, there doesn’t have to be a “real” method called AnotherTest() at all.

This type of binding is also called “Custom Binding”; Custom binding occurs when a dynamic object implements IDynamicMetaObjectProvider (IDMOP). I’ll write more about custom binding sometime.

If you’d like to read more, here is a worth viewing, if you are short of reading time, article C# 4.0 – Dynamic Language Runtime by Microsoft’s Bruno Terkaly with excellent visuals.