Dynamic Type in C#

As the dynamic languages such as IronPython and IronRuby are gaining popularity, Microsoft has introduced dynamic language runtime (DLR) in .NET Framework 4. With DLR one can supports the dynamic type in C#. Now let us explore the usage dynamic in c#



As the dynamic languages such as IronPython and IronRuby are gaining popularity, Microsoft has introduced dynamic language runtime (DLR) in .NET Framework 4. With DLR one can supports the dynamic type in C#. Now let us explore the usage dynamic in c#.

In c# with .Net 4.0 there is new type dynamic. Let us see how we can use it.

Create a new class with one method GetClassName()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class DynamicExample
    {

        public string GetClassName()
        {
            return "DynamicExample";
        }
 
    }
}

Now create a window form and insert one button to it and write the code in button_click

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                dynamic de = null;
                de = new DynamicExample();
                string s1= de.GetClassName();
                Console.WriteLine(s1);

                //call the method which does not exist, no compile time error
                string s2 = de.GetClassName1();// this will throw the exception during rum time
                 Console.WriteLine(s2);
        }

     }
}

Now let us examine the code line by line

One dynamic type variable is declared and new instance of DynamicClass is added to it. Then call is made to GetClassName() method of DynamicClass class.
According to Microsoft , an element that is typed as dynamic is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program.

Console.WriteLine(s1) will write a line "DynamicExample" in console. Which is what we expected.

But care should be taken while calling the function through dynamic type. If you call the method which does not exists or have wrong parameters. It will not show compile time error, you will get the error during run time.

//call the method which does not exist, no compile time error
string s2 = de.GetClassName1();// this will throw the exception during rum time

This line will throw runtime exception;

fig.gif

Don't get confuse with "var" type . In case of var, error is shown at compile time, while in dynamic error is at run time. Moreover, use of dynamic is not limited to call the CLR code only. You can also call the code from IronPython and IronRuby. if you are interested please follow the following link http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx