09 Sep

Dynamic Type in C# 4.0

This example shows how to use dynamic type in C#.

C# 4.0 allows a new static type called “dynamic”. Any operation on the object of type dynamic resolve at runtime. So, it gets escaped from compile type checking. But if any error, it caught at runtime. Also, the dynamic type allows us to access the object, without knowing type of the object at compile time. It causes no intelli-sense support.

Sample Usage :

            dynamic dynamicObj = "This is a dynamic type";
            Type dynObjType = dynamicObj.GetType();
            //This is a dynamic type

In above code, a variable of type dynamic is defined. This type is resolved in type string at runtime. We are printing the string value, and type.

Leave a Reply

Your email address will not be published. Required fields are marked *