Introduction to Dynamic Language Runtime and .NET 4.0

Introduction to Dynamic Language Runtime (DLR)

The Dynamic Language Runtime (DLR) from Microsoft is an effort to bring a set of services that run on top of the Common Language Runtime (CLR) , in upcoming .NET Framework 4.0 has this built within in it and provides language services for several different dynamic languages.

These services include:

  • A dynamic type system, to be shared by all languages utilizing the DLR services
  • Dynamic method dispatch
  • Dynamic code generation
  • Hosting API

The DLR is used to implement dynamic languages like Python and Ruby on the .NET Framework. The DLR services are currently used in the development version of IronRuby, a .NET implementation of the Ruby language, and for IronPython and Silverlight application development using IronPython.


C# 4.0 supports dynamic programming by introducing new dynamic typed objects. The type of these objects is resolved at run-time instead of at compile-time. The keyword tells the compiler that everything to do with the object, declared as dynamic, should be done dynamically at the run-time using Dynamic Language Runtime(DLR). The main advantages of this dynamic programming are it provides more flexibility to developers.

The DLR has been a part of upcoming .NET 4.0 release. The new dynamic functionality in .NET 4.0 allows us to work with types not known at compile time in an intuitive and easy to read way.

The dynamic changes allow us to do the following

1.) Write more readable code with fewer casting opeations.

2.) create new languages to utilize the .NET framework, such as IronPython and IronRuby.  Additionally, .NET’s dynamic architecture allows these languages to automatically benefit from future framework advances.

3.) Utilize other dynamic languages and their libraries from C# and VB.NET. 

4.) Work with COM objects more easily.

Static vs Dynamic

STATIC: In a statically typed language, such as C# or C, the compiler checks you are using types correctly at compile time. Compilation will fail, if there is light mismatch in your code.

For Example: you are assigning an integer to string.  The .NET compiler will generate an error when you try to compile your source code.

This is because the statically typed languages can catch simple syntax errors and typos during development as compiler knows the types it will be working with static languages and generally run quicker than dynamic.


The dynamic languages such as JavaScript, Python, Lisp and Ruby, do not perform typec checking at the compile time, the type checking will  be performed at the runtime. This will be a big advantage if you don’t know the type of the object you are going to process/use until the runtime.

The dynamic languages are useful when you want to implement a new language or language interoperability across your .net languages. You can consume the code written in IronPython in your C# or VB.NET code.  Seems cool to hear right, we don’t need dynamic language until you came across a situation where your application needs such a functionality. But it is a great addition to our .NET Framwork family.

Disadvantages of Dynamic Languages 

There are some disadvantages of working with Dynamic languages.

1.)  Lower performance than statically typed languages, because the expressions are evaluated in runtime, which would make the application performance to go slower than the statically typed languages.

2.) When working with dynamic languages, a simple syntax error or typos can stop your code working. For Example. When we are working with JavaScript you all might have encountered a “object noy set to an instance” error, on which you spend hours and hours to debug and identify where the exception is occuring. Same will happen in the case of dynamic languages.

The new type “Dynamic”

The .NET 4.0 comes with a new type “dynamic” that allows you to tell the compiler to resolve a variable’s type at runtime. The keyword “dynamic” statically types the object as dynamic. For making an object  dynamic all you have to do is prefic the variable name with the type dynamic.

For Example.

dynamic myDynObj = 1;

Console.WriteLine(myDynObj.GetType().Name) ;

It will print “int” or “Int32”.

Var keyword vs dynamic keyword – A common question  – Aren’t  they the same?

The answer is NO. both are entirely different logically and implementation wise. 

When you declare a Variable as “var”,  the compiler automatically picks up the type of the object you have assigned to the variable. The compiler knows exactle the type of the value you assigned to the variable. and even Visual Studio IDE provides intellisense to it’s method and properties.

But when you declare a variable as “dynamic”. The compiler and Visual Studio have no idea what kind of or what is the type of data you have assigned to the variable until the runtime. 

Remember type delcared as “dynamic” will be evaluated in the “Application Run-Time”, while types declared as “var” will be evaluated at the  ”Compile Time”.

Consider using the Dynamic Types in the following situations.

  • When working with COM, dynamic types allow a more concise syntax. Let the DLR do the work figuring out how to bing your merhod and property calls.
  • When interacting with a dynamic language such as IronPython.
  • When working with objects that have changing structures, such as “HTML” or “XML” documents.

That’s all for now in this time. Will write more finding when i get more on dynamic.

Make your self aware of DLR and dynamic in .NET 4.0 from following links

MSDN Links

Channel 9 Links here

Scott Hanselman’s nice article here

Nikhil Kothari’s WebLog

Geeks WebLogs

Alex Mackey’s – Apress Pro – Introducing .NET 4.0

and more you can find through a google search.

Have a nice time and happy learning!!!!!

One thought on “Introduction to Dynamic Language Runtime and .NET 4.0

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.