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(); Console.WriteLine(dynObjType); Console.WriteLine(dynamicObj); //Result: //System.String //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.