Dynamic Type in C# 4.0- New Feature

No.of Views830
Bookmarked0 times
Downloads 
Votes0
By  Ravish Kumar Sindhwani   On  11 Jun 2010 21:06:39
Tag : CSharp , CSharp4.0
Dynamic Type is a part of DLR (Dynamic Language Runtime). This DLR is nothing but some added feature to common language runtime.a
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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 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 

Image Loading

This is all about Dynamic type.i hope this is help to you all.

 
Sign Up to vote for this article
 
About Author
 
Ravish Kumar Sindhwani
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-11 Jun 2010
Home Page-
Blog Page-
I born and brought up in Haryana and working as a software developer in Trivandrum. I am passionate for learning new Microsoft technologies and I believe if you are learning any new thing, just write it some where. You will never forget.
 
 
Other popularSectionarticles
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
    Through this article, i will introduce in FCL named Tuple which can store n - number of values in it. Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you
    Published Date : 15/Nov/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top