Dynamic Type In C# 4.0

This article will introduce dynamic type in C#. The code examples in this article demonstrate different usage of dynamic type and dynamic programming of .NET in C#.

Objective

Dynamic programming was introduced in C# version 4.0. This article and code examples in this article introduce dynamic programming and dynamic type in C# language. 

C# 4.0 is all about Dynamic Programming and type.

Dynamic type in C#


10 facts about Dynamic type

 

  1. C# 4.0 allows a new static type called "dynamic".
  2. Any operation on the object of type dynamic resolve at runtime.
  3. The dynamic allows us to access the object, without knowing type of the object at compile time.
  4. Dynamic types are internally treated as System.Object.
  5. At compile time we declare a dynamic type but at run time we get a strongly typed object.
  6. Dynamic type gets escaped from compile type checking. But if any error, it caught at runtime.
  7. Dynamic type is assumed to perform any task at compile time.
  8. Variable of type dynamic compiled into type object. So, after compilation dynamic does not exist.
  9. Dynamic type does not support intelli-sense.
  10. Object of any language type could be treated as dynamic type provided they are capable of naturally dispatch them.
Usage

This is used to call and consume object of dynamic type languages like Python and Ruby. Now C# could call object of any dynamic language in the type dynamic.

Sample 1

Dynamic type in c#

Program.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace DynamicTypeSample1  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             dynamic dobj = "Hi I am Dynamic ";  
  12.             Console.WriteLine(dobj);  
  13.             Console.ReadKey(true);  
  14.         }  
  15.     }  
Dynamic type in c#

Explanation
  1. In above code, I am defining a variable dobj of type dynamic.
  2. This type is resolved in type string at runtime.
  3. We are printing the string value. 
To get the runtime type of the dynamic type, I am adding one line of code in above code as
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         dynamic dobj = "Hi I am Dynamic ";  
  6.         Type dobjtype = dobj.GetType();  
  7.         Console.WriteLine(dobjtype);  
  8.         Console.WriteLine(dobj);  
  9.         Console.ReadKey(true);  
  10.     }  
  11. }
Dynamic type in c#

Explanation
  1. GetType() operation on dynamic type is use to get the runtime type of the dynamic type.
  2. As output, we are getting System.String. This is runtime type of the dynamic type.
Dynamic type could be used with
  1. Local type.
  2. Function return type
  3. Properties
  4. Indexers
  5. Class type
  6. Function argument
  7. Type constraint.
Conversion on dynamic type
 
Any object type can be converted into dynamic type implicitly.
  1. dynamic dobj = 9;  
  2. dynamic dobj1 = " Dhananjay Kumar";  
  3. dynamic dobj3 = System.DateTime.Today;   
Dynamic type could also get converted into any type implicitly 
  1. int i = dobj;  
  2. string s = dobj1;  
  3. string sd = dobj3;   
Sample 2

In this sample

  1. We will make a class SampleForDynamimc.
  2. There are two properties, one is of type string and another is of type dynamic.
  3. There is one method, and return type of this method is dynamic and it is taking an input parameter of the type dynamic.
  4. In Main class, we are making object of type dynamic. This is getting resolved SampleForDynamic type at run time.
  5. Calling the dynamic type method with dynamic type argument.
  6. Printing the output.
SampleForDynamic class

Dynamic type in c#

Operations are resolved at run time. So there is no information about operations or properties are available at run time.

Dynamic type in c#

Program.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace DynamicTypeSample1  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             dynamic obj = new SampleForDynamimc();  
  12.             obj.Name = " Mahesh Chand";  
  13.             obj.Sex = "Male";  
  14.             dynamic message =" Welcome to Dynamimc World ";  
  15.             dynamic result = obj.Getinformation(message);  
  16.             Console.WriteLine(result);  
  17.             Console.ReadKey(true);  
  18.         }  
  19.     }  
  20.   
  21.     public class SampleForDynamimc  
  22.     {  
  23.         public dynamic Name { getset; }  
  24.         public string Sex { getset; }  
  25.         public dynamic Getinformation(dynamic param1)  
  26.         {  
  27.             return Name +"\t" + Sex + "\t"+ param1 ;   
  28.         }  
  29.     }  
  30. }
Dynamic type in c#

Conclusion

I have talked about, dynamic type of C# 4.0. I've also shown two samples also on how to use dynamic types.

Thanks for reading my work.

Till next reading, Happy Coding.