Dynamic Data Type In C#

Dynamic data type was introduced with C# 4.0 in Visual Studio 2010. Dynamic data types are dynamic in nature and don’t require initialization at the time of declaration. We can define this data type using the keyword “dynamic" in our code. A variable of the dynamic data type can be initialized by any type of data like int, float ,string or object. Dynamic variables can be used to create properties and return values from a function.

Example

  1. class Demo  
  2. {  
  3.     dynamic Data = 12;  
  4.     public int Method(int A, int B)  
  5.     {  
  6.         return (A + B) * Data;  
  7.     }  
  8. }  
  9. class Program  
  10. {  
  11.     static void Main(string[] args)  
  12.     {  
  13.         DemoObj = newDemo();  
  14.         dynamic value1 = 10;  
  15.         dynamic value2 = 11;  
  16.         dynamic Str = "Your Result Is: ";  
  17.         Console.WriteLine(Str + Obj.Method(value1, value2));  
  18.         Console.ReadLine();  
  19.     }  
  20. }  
Output

result

Expression for dynamic data types is evaluated at run time, it means all the member functions and the member variables that are associated with dynamic keywords are ignored at the compile time. The compiler preserves all the information about the expression, such as the data types, and uses it later to evaluate the expression at the runtime.

Example:
  1. class Demo  
  2. {  
  3.     dynamic Data = 12;  
  4.     public int Method(int A, int B)  
  5.     {  
  6.         return (A + B) * Data;  
  7.     }  
  8. }  
  9. class Program  
  10. {  
  11.     static void Main(string[] args)  
  12.     {  
  13.         //Obj As A Object of Demo Class  
  14.         dynamicObj = newDemo();  
  15.         dynamic value1 = 10;  
  16.         dynamic value2 = 11;  
  17.         dynamicStr = "Your Result Is: ";  
  18.         Console.WriteLine(Str + Obj.Method(value1, value2));  
  19.         //Obj as a String  
  20.         Obj = "Pankaj Kumar Choudhary";  
  21.         Console.WriteLine(Obj);  
  22.         //Obj as a Integer  
  23.         Obj = 12345;  
  24.         Console.WriteLine(Obj);  
  25.         //Obj as a Float  
  26.         Obj = 123.45;  
  27.         Console.WriteLine(Obj);  
  28.         //Obj as a Boolean  
  29.         Obj = false;  
  30.         Console.WriteLine(Obj);  
  31.         //Obj as a Dictionary  
  32.         Obj = newDictionary < int, String > ();  
  33.         Obj[0] = "Haryana";  
  34.         Obj[1] = "Rajasthan";  
  35.         Obj[2] = "Punjab";  
  36.         Obj[3] = "Goa";  
  37.         foreach(var index inObj.Keys)  
  38.         {  
  39.             Console.WriteLine(Obj[index]);  
  40.         }  
  41.         Console.ReadLine();  
  42.     }  
  43. }  
Output

see output

We can change the types for a dynamic data type. In the above example we assign the int, float, string, object and types for dynamic data type(obj). Size of dynamic data type is calculated the values that is assigned for dynamic data type.

Conversion

Conversion b/w dynamic type and implicit data type is very easy. It enables the developer to switch between dynamic and non-dynamic behavior.

Example:
  1. class Demo  
  2. {  
  3.     dynamic Data = 12;  
  4.     public int Method(int A, int B)  
  5.     {  
  6.         return (A + B) * Data;  
  7.     }  
  8. }  
  9. class Program  
  10. {  
  11.     static void Main(string[] args)  
  12.     {  
  13.         //Conversion From Implicit Type To Dynamic Type  
  14.         dynamicObj = new Demo();  
  15.         dynamic value1 = 10;  
  16.         dynamic value2 = 112.45;  
  17.         dynamicStr = "This is a string";  
  18.         //Conversion From Dynamic Type To Implicit Type  
  19.         Demodm = Obj;  
  20.         int int_ = value1;  
  21.         double ft = value2;  
  22.         string Str_ = Str;  
  23.         Console.WriteLine(Obj.Method(100, 20));  
  24.         Console.WriteLine(value1);  
  25.         Console.WriteLine(value2);  
  26.         Console.WriteLine(Str);  
  27.         Console.WriteLine(dm.Method(11, 12));  
  28.         Console.WriteLine(int_);  
  29.         Console.WriteLine(ft);  
  30.         Console.WriteLine(Str_);  
  31.         Console.ReadLine();  
  32.     }  
  33. }  
Output

run

Overload Resolution with Arguments of Type Dynamic

Overload resolution occurs at run time instead of at compile time if one or more of the arguments in a method call have the type dynamic. In the below example in the main method we are passing two dynamic data types in Method. In Demo class all argument of Call method are integers but in the main method weare  passing two dynamic data types in which the first dynamic type(value1) contains string value, so an error will occur at run time instead of compile time.

Example:
  1. class Demo  
  2. {  
  3.     dynamic Data = 12;  
  4.     public int Call(int A, int B)  
  5.     {  
  6.         return (A + B) * Data;  
  7.     }  
  8. }  
  9. class Program  
  10. {  
  11.     static void Main(string[] args)  
  12.     {  
  13.         dynamic Obj = newDemo();  
  14.         dynamic value1 = "10";  
  15.         dynamic value2 = 112;  
  16.         Console.WriteLine(Obj.Call(value1, value2));  
  17.         Console.ReadLine();  
  18.     }  
  19. }  
Output

Output

Difference between var and dynamic keyword

The var keyword can be used to store any data type value. The var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. The var keyword was introduced in C# 3.0 and dynamic was introduced in C# 4.0. Let us consider some differences between both keywords.

Statically Typed and Dynamically Typed

This means the type of variable declared for var is decided by the compiler at compile time but the type of dynamic variable declared is decided by the compiler at runtime. So type of var variable is known at compile time so error for var variable is generated at compile time but in case of dynamic, type of variable is unknown till runtime so error is generated for dynamic time variable at run time.

Need of Initialization

Need of initialization for var is required at the time of declaration; if we don’t initialize the value we will get an error but in the case of dynamicthere is no need to initialize at the time of declaration.

Need of Initialization

Multiple Initialization with different Data Type

We can not change the data type for a var keyword. It means if we assign the integer value at time of declaration then further we can assign string, double,  or other data type value for var keyword. If we try to do so then compiler will throw an error. In case of dynamic type there are no such types of restrictions, we can assign different types of value for dynamic type variable.

Multiple Initialization

Function Parameter

We can’t use the var keyword as a parameter for any function but the dynamic type can be used as parameter for any function .

Function Parameter

Return Type

We can’t create a method with var as return type but we can create a method whose return type is dynamic.

Error

Conclusion

Dynamic Type is a nice feature to use dynamic language runtime. However, dynamic type also makes it the responsibility of the developer for the correct usage of the calls.
 
Read more articles on C# Programming:

 

Up Next
    Ebook Download
    View all
    Learn
    View all