Wednesday, May 18, 2011

Dynamic Type (New Feature in C# 4.0)

Dynamic Type is a part of DLR (Dynamic Language Runtime). This DLR is nothing but some added feature to common language runtime.
For using DLR features we need to add System.Dynamic namespace.

Dynamic type is the feature of C# 4.0. It is a powerful feature of 4.0 framework which allows us to write code in such a way that we can bypass compile time checking. Here bypass doesn’t mean that we can remove the compile time errors, it means if the operation is not valid than we cannot detect the error at compile time. This error will appear only in run time.
Difference between var keyword and dynamic keyword:
The objects defined with dynamic keyword can change its type at runtime. But it is not possible with the var keyword. When we use var keyword it means the determination of the type is delayed but it will happen only in compile time. It means once the type has been specified than we cannot change the type of the object declared using var keyword.
And in the case of dynamic we can change its type in run time and as many times as required.
In the following example we will see how we can use dynamic objects:
static void Main(string[] args)
        {
            dynamic dynamicVariable;
            var oMyClass = new MyClass();
            dynamicVariable = oMyClass.DisplayGreeting();
            Console.WriteLine(dynamicVariable+ "\n\nand the type of dynamic variable is: "+ dynamicVariable.GetType());

            dynamicVariable = oMyClass.DisplayInteger();
            Console.WriteLine("\n" + dynamicVariable + "\n\nand the type of dynamic variable is: " + dynamicVariable.GetType());
            Console.Read();
        }

        class MyClass
        {
            private string sName = "Jack";
            private string sGreeting = "Good Morning";
            public string DisplayGreeting()
            {
                return (sGreeting + sName);
            }
            public int DisplayInteger()
            {
                return 5;
            }
        }

Here in this example we have MyClass containing two functions ‘DisplayGreeting’ and ‘DisplayInteger’.  Both functions are returing different values.
In our main function we have declared a dynamic variable ‘dynamicVariable’.
dynamicVariable = oMyClass.DisplayGreeting();
            Console.WriteLine(dynamicVariable+ "\n\nand the type of dynamic variable is: "+ dynamicVariable.GetType());

Here in the above line we are assigning string value to it and printing it in console, value as well as its type.
In the below mentioned line line we are assigning string value to it and printing it in console, value as well as its type.
            dynamicVariable = oMyClass.DisplayInteger();
            Console.WriteLine("\n" + dynamicVariable + "\n\nand the type of dynamic variable is: " + dynamicVariable.GetType());


When we are compiling it than we are getting the output as:
clip_image002
This is all about Dynamic type.

Thank You.
Ravish

No comments:

Post a Comment