Click here to Skip to main content
13,356,318 members (63,864 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 3 Oct 2010

Introduction - DLR (Dynamic Language Runtime)

, 4 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Using dynamic types in Microsoft Visual Studio .NET (Framework 4.0)


CLR (Common language runtime) is nothing new now, .NET developers are familiar with the CLR, how to work with the object. There is no doubt that now we can design our component and application easily using CLR.

We will not discuss about the CLR. We will try to focus on the DLR ((Dynamic Language Runtime). At first, we need to know what is DLR. In a simply world, we can say that the DLR has some new features which added to common language runtime (CLR) for Microsoft .NET Framework 4.0.

More information about CLR can be found here.

What Dynamic Types Do

Let’s try to get some more. One of the powerful features of Microsoft .NET Framework 4.0 is the Dynamic Types.

Dynamic Types allow us to write code in such a way that we can go around compile time checking. Note that bypass does not mean that we can remove the compile time errors, it means if the operation is not valid, then we cannot detect the error at compile time. This error will appear only in run time.

How to Work with Dynamic Types

Microsoft .NET Framework 4.0 provides us the System.Dynamic namespace. To use Dynamic Types, we need to add the references DLL of System.Dynamic.

Figure: Showing the dynamic type of current object output.


Well , I hope that you get the basics of DLR ((Dynamic Language Runtime), so let's try to play with a few code snippets and a few keywords of Microsoft Visual Studio .NET. We are familiar with the keyword “var” we will play with this keyword, before that one interesting thing I must share is that it is possible to change an object type runtime? Probably your answer will be NO. But objects defined with dynamic keyword can change their type at runtime. Isn't it cool. A simple code example is given below:

More information about System.Dynamic can be found here.

Using the Code

Here in this example below, we have a class called Class1(). The Class1() has two functions displayMessage() and displayNumeric(). Both functions are returning different values.

static void Main(string[] args)
     dynamic myDynamicVariable;
     // "dynamic" is keyword for declaring dynamic type variables 
     var _objClass = new Class1();
     // Creating object 

     myDynamicVariable = _objClass.displayMessage();
     Console.WriteLine("Value: " + myDynamicVariable + ". 
	Type of dynamic variable is: " + myDynamicVariable.GetType());
     // Displaying types
     myDynamicVariable = _objClass.displayNumeric();
     Console.WriteLine("\nValue: My CP Member Id # " + myDynamicVariable + 
	". Type of dynamic variable is: " + myDynamicVariable.GetType());
     // Displaying types
        class Class1
            private string sMessage = "The Code Project is COOL";
            public string displayMessage()
                return sMessage;
            public int displayNumeric()
                return 1186309;

If you look, we have declared a dynamic variable myDynamicVariable.

dynamic myDynamicVariable;
// "dynamic" is keyword for declaring dynamic type variables 

So we are assigning string value to it and printing it in console, value as well as its type. This is all about Dynamic type.


I hope this might be helpful to you. Enjoy!


  • 4th October 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Marufuzzaman
Bangladesh Bangladesh
A well experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by and also selected as a mentor of Published over 100 technical articles in various software development resource sites (i.e.,, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over twelve years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.I have also more than two years’ of strong experience in mobile-VAS (platform development).

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint.Honest, believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, Research, MIS, Data Analytics, Data Mining, BI, SaaS platform base application development, Large scale Win32 Form/Web based business software solutions, Security, Enterprise applications development, integration, etc.

Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12/14 , Oracle version 10/11, MySql version 5.1, 5.5, 5.7, Visual C#, VB.NET, ASP.NET, API, MVC, WebAPI , WPF, WCF, PHP, Microsoft Power BI, SPSS, PS2, R, Add-In, Visual Basic etc.

.Net UI component: Telerik, DevExpress, Ext.Net.
Scripting language: JavaScript, AngularJS, node.JS.
Source control / Subversion: Git,SVN.
Development methodologies: Agile,RAD.
Project Management / Issues Tracking Tools: JIRA, ClockingIt.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
VishalRajut22-Aug-13 20:29
memberVishalRajut22-Aug-13 20:29 
Question[My vote of 1] it's too simple Pin
yudha_hyp20-Feb-12 18:09
memberyudha_hyp20-Feb-12 18:09 
GeneralMy vote of 2 Pin
afaz28-Dec-10 2:32
memberafaz28-Dec-10 2:32 
GeneralMy vote of 1 Pin
pvandijk2819-Oct-10 2:06
memberpvandijk2819-Oct-10 2:06 
GeneralMy vote of 1 Pin
Yury Goltsman13-Oct-10 22:11
memberYury Goltsman13-Oct-10 22:11 
GeneralMy vote of 2 Pin
Gishu Pillai12-Oct-10 21:29
memberGishu Pillai12-Oct-10 21:29 
GeneralMy vote of 2 Pin
fire_birdie11-Oct-10 20:33
memberfire_birdie11-Oct-10 20:33 
GeneralMy vote of 1 Pin
Andrey Mazoulnitsyn11-Oct-10 20:28
memberAndrey Mazoulnitsyn11-Oct-10 20:28 
bad article
GeneralMy vote of 2 Pin
vytheese11-Oct-10 19:55
membervytheese11-Oct-10 19:55 
GeneralMy vote of 2 Pin
Daniel Brännström9-Oct-10 5:20
memberDaniel Brännström9-Oct-10 5:20 
GeneralMy vote of 2 Pin
andrew45826-Oct-10 5:21
memberandrew45826-Oct-10 5:21 
GeneralMy vote of 2 Pin
John Brett4-Oct-10 23:59
memberJohn Brett4-Oct-10 23:59 
GeneralRe: My vote of 2 Pin
Md. Marufuzzaman5-Oct-10 7:06
mvpMd. Marufuzzaman5-Oct-10 7:06 
GeneralMy vote of 2 Pin
Michael B. Hansen4-Oct-10 21:41
memberMichael B. Hansen4-Oct-10 21:41 
GeneralRe: My vote of 2 Pin
Md. Marufuzzaman4-Oct-10 23:00
mvpMd. Marufuzzaman4-Oct-10 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 4 Oct 2010
Article Copyright 2010 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid