What’s new in C# 4.0 – dynamic programming


C# version 4.0 that comes with .NET Framework 4.0 and VS 2010 comes with some new language features and enhancements. Though the major in the list of new features surely would be dynamic programming and DLR, other features are also of importance. Here’s a list of the important new features introduced in C# 4.0 –

·         Late Binding/Dynamic Programming Support through DLR and dynamic keyword
·         Named and Optional Parameters
·         Improved COM Interop
·         Covariance and Contravariance
·         Co-evolving C# and VB – bringing down the gap between C# and VB languages

In this blog post I would discuss about Dynamic programming / Late Binding support in C# 4.0 with dynamic keyword. I would discuss about other features in future blog posts.

Late Binding/Dynamic Programming Support through DLR and dynamic keyword

From the inception days itself C# has been designed and architected as a Static programming language – that is binding to all method calls are done at compile time by the compiler. If the C# compiler does not find a suitable/appropriate method during phase, it gives a compilation error – that’s been the story from day 1 of C# 1.0 (rather from beta days). So the following lines code would give a compile time error –

 

On the other hand VB.NET has supported late binding from day 1 because of it’s history and acquaintance with Basic, Visual Basic. So it is not as statically typed as C# is – and in this scenario in allows us to do late binding. So the corresponding piece of code in VB.NET compiles and shows an output of 11

C# 4.0 introduces a new keyword called dynamic that supports late binding – keep in mind that dynamic is a static type in itself. So now rather than using object we can use dynamic. This does not mean that C# is now going away from static typing. When the compiler finds dynamic it uses DLR (Dynamic Language Runtime) to bind the method calls instead of C# Object Binder.

This code snippet compiles successfully and produces a correct output of 11. One interesting info: when dynamic keyword is used, VS 2010 IDE recognizes it and just tells that the operation would be resolved at run time (rather than showing members, as it could not know what the members are).

 

If the member is not found at runtime (i.e. DLR could not bind/resolve to the mentioned member name), a RuntimeBinderException is thrown –

 

It’s also possible to use dynamic type (which is a static type) as method parameter and return type –

static void Main(string[] args)

{

string str = "Hello from dynamic";
int len = CalculateLength(str);
Console.WriteLine("Length is {0}", len);
Console.ReadLine();

}

private static dynamic CalculateLength(dynamic value)

{

return value.Length;

}

It’s also possible to declare dynamic variables at class level –

static

dynamic d = "Hello from dynamic";

static void Main(string[] args)

{

int len = d.Length;
Console.WriteLine("Length is {0}", len);
Console.ReadLine();

}

As I mentioned, DLR helps languages bring late binding into .NET world as CLR is primarily designed for static-typing. Here’s how languages Dynamic languages like IronPython, IronRuby and CLR based languages like C#, VB.NET uses DLR –


Without DLR scenario:

With DLR:

 

So as you can understand one of the main concepts behind C# 4 is dynamic programming and dynamic languages. If you remember in C# 3.0 it was functional programming with local variable inference, automatic properties, Lambda expressions etc. So with both the releases of C# 3.0 and 4.0, C# team has caught up with the language trends –

 

So here’s a overall picture of the C# journey till date –

There are lot of resources about dynamic/DLR. Following are some –

http://blogs.msdn.com/samng/archive/2008/10/29/dynamic-in-c.aspx
http://blogs.msdn.com/cburrows/archive/2008/10/27/c-dynamic.aspx

Keep Learning C# 4.0 and .NET 4 – Aniruddha

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s