SIGN UP MEMBER LOGIN:    
ARTICLE

Dynamic Type in C#

Posted by Anand Thakur Articles | C# Language September 22, 2010
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#
Reader Level:


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

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you for sharing Anand.

Posted by Mahesh Chand Sep 22, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor