Dynamic Type In C#

In this article, I will discuss Dynamic Type in C#,
 
Dynamic Type
 
'Dynamic' keyword is used to declare the variable of the dynamic type. Dynamic data type variable can contain any type of value. Type of value contained is checked at runtime. The syntax of Dynamic type is shown below:
 
Dynamic <VariableName> = value;
 
'Dynamic' keyword is similar to 'var' keyword or you can think it as a replacement for var keyword. Unlike 'var', it is not necessary to initialize dynamic variable while declaring. Dynamic types are used in many cases as shown below:
 
Multiple declarations of a variable with different data types
With dynamic, we can have multiple initializations of a variable of different data types. Like a variable named 'sample' can contain int, float, bool, string etc. values in a class as shown below in the example:
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("hello Tutpoint");  
  10.   
  11.             dynamic sample;  
  12.             sample = 12;  
  13.             Console.WriteLine(sample);  
  14.   
  15.             sample = 0.222;  
  16.             Console.WriteLine(sample);  
  17.   
  18.             sample = true;  
  19.             Console.WriteLine(sample);  
  20.   
  21.             sample = "Dynamic";  
  22.             Console.WriteLine(sample);  
  23.   
  24.             sample = null;  
  25.             Console.ReadLine();  
  26.         }  
  27.   
  28.   
  29.     }  
  30.   
  31. }  
An output of the program will be:
  1. hello Tutpoint  
  2. 12  
  3. 0.222  
  4. True  
  5. Dynamic  
Dynamic return type and/or a dynamic parameter
 
This is the most important and awesome feature of dynamic. With this, a method can return value of different data type as shown in the example.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("hello Tutpoint");  
  10.   
  11.             dynamic sample;  
  12.             sample = 12;  
  13.             Console.WriteLine(sample);  
  14.   
  15.             sample = 0.222;  
  16.             Console.WriteLine(sample);  
  17.   
  18.             sample = true;  
  19.             Console.WriteLine(sample);  
  20.   
  21.             sample = "Dynamic";  
  22.             Console.WriteLine(sample);  
  23.   
  24.             sample = null;  
  25.             Console.ReadLine();  
  26.         }  
  27.   
  28.         public dynamic TutLovers(dynamic dynm)  
  29.         {  
  30.             if (dynm == 100)  
  31.                 return 100;  
  32.             else if (dynm == "Dynamic")  
  33.                 return "Dynamic";  
  34.             else if (dynm == 12)  
  35.                 return 12;  
  36.             else  
  37.                 return false;  
  38.         }  
  39.   
  40.   
  41.     }  
  42.   
  43. }