ISerialized .Net, C#, Scrum and agile software development

2Feb/101

Dynamic Language Runtime in .Net 4.0

The upcoming .Net 4.0 contains many new and exciting features, in this blog post I will describe some of the new features of the DLR including the dynamic keyword.

To quote MSDN:

The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages.

The architecture of the DLR in combination with the Common Language Runtime CLR and C#, IronPython, IronRuby and Visual Basic is visualized through this illustration:

As seen from the illustration, the DLR adds three new and important features to the CLR:

  • Expression Trees
  • Call Site Caching
  • Dynamic Object Interoperability
As the .Net Framework 4.0 is still in beta, I have intentionally left out the links to MSDN library, as the features and links might change from the beta documentation to the final version.
Two quick examples of usage

Take a look at the following code for incrementing a counter in XML:

Scriptobj.SetProperty("Count", ((int)GetProperty("Count")) + 1);

The same operation in the DLR is solved with the following expression:

scriptobj.Count += 1;

Another cool feature of .Net 4.0 is the dynamic keyword.
The dynamic keyword has many similarities with the object keyword, but on of the most interesting differences is the compilers interference. Working with object, the compiler tells us that we are not allowed to operate without explicit casts, eg:

object a = 1;
a = (int) a + 5;

If we try to remove the explicit cast to int here, the compiler would complain and our code would not compile.
With dynamic on the other hand, I can do the following operation

dynamic a = 1;
a = a + 5;

The compiler will not interfere with this at compile time as we are using the dynamic keyword, and at runt time this will be handled as Int32. The dynamic keyword will allow us to do such operations on all types that supports the + keyword.

We can also perform the following:

dynamic a = 1;
a = a + 5;

a = "6"
a = a + 6;

On line 4 above, I reassign the a to be of type String.

One thing to notice though, is that if I have a dynamic a, assign in the value 1, and try to pass it to a method requiring a string as parameter, I will get an exception at run time, and the dynamic contans wrong type, and the method will never be called. If I however assign it the value "1" it will work fine, as the dynamic the contains the same type as the method requires.

About Paul Eie

My name is Pål Eie (English alias: Paul Eie) I'm an IT consultant working in Stavanger, Norway. My experience spans from AIX to Windows and from embedded C to Uniface, Java, C++ and C#. My blog will involve everything that is related to technology. As long as it might be of interest to either newbies or seniors, I will blog about it! If you find some of my posts about basic old technology, it just means you know more than many of my other readers!
Comments (1) Trackbacks (0)

Leave a comment


No trackbacks yet.

Blogglisten