Saturday, April 18, 2020

Dynamic Keyword in C#

C# is a strongly-typed language. What does it mean? It means, in C#, every variable and constant has a pre-defined type. Before you use any variable, you must tell the compiler what type of value a variable will store.
Let's talk a real-world example.
Our world is filled with the containers. A container can be a truck carrying dirt. A container can be a milk box or even a cup of tea. Some of the containers in our world are pre-defined what they can store and what not. Let's call them predefined containers. For example, a gasoline container is usually designed to carry gasoline. But most of the containers in our world are multi-purpose. For example, in a bowl, we can store water, milk, tea or even sugar. Depending on the size of the container and fruits, we can even store some fruits in a bowl. So, in this case, a bowl is a all-purpose container. To compare with the real world, in C# language, most of the constants and variables are predefined containers. But what if you want to use a variable as an all-purpose container?
What if you are not sure of the type of value you want to store in a variable? This is where dynamic programming is plays a vital role. Dynamic programming is supported by the dynamic keyword that was introduced in C# 4.0. If you're working with previous versions of C# language, you will not be able to use the dynamic keyword. 
The dynamic keyword is used to declare dynamic types. The dynamic types tell the compiler that the object is defined as dynamic and skip type-checking at compiler time; delay type-checking until runtime. All syntaxes are checked and errors are thrown at runtime. The dynamic programming was introduced to provide a cross reference between .NET and non .NET APIs such as COM APIs, IronPython, and the COM version of the HTML Document Object Model (DOM).
The following code snippet declares a dynamic field, a dynamic property, a dynamic method and a dynamic variable.
  1. // Define a dynamic field.  
  2. dynamic name;  
  3. // Define a dynamic property.  
  4. dynamic NameProperty { getset; }  
  5. // Define a dynamic method with a dynamic parameter type.  
  6. public dynamic FullNameMethod(dynamic d)  
  7. {  
  8.   name = "Mahesh";  
  9.   dynamic firstname = name;  
  10.   string lastname = "Chand";  
  11.   return firstname + lastname;  
  12. }  
As you may have noticed from the code above, a dynamic variable can store any type of value. A method can also return a dynamic type and also can accept dynamic parameters.
Here is an example with a dynamic variable that can store a string, an integer, a double, and a Boolean value. 
  1. dynamic dyno = "Mahesh Chand";  
  2. Console.WriteLine(dyno);  
  3. dyno = 38;  
  4. Console.WriteLine(dyno);  
  5. dyno = 44.95;  
  6. System.Console.WriteLine(dyno);  
  7. dyno = true;  
  8. System.Console.WriteLine(dyno);  
Dynamic types can also be converted explicitly to other types and vice versa. The following code snippet converts a string, an integer, and a double type into dynamic types by casting them with the dynamic keyword. 
  1. dynamic d;  
  2. string name = "Mahesh Chand";  
  3. d = (dynamic)name;  
  4. Console.WriteLine(d);  
  5.               
  6. int age = 38;  
  7. d = (dynamic)age;  
  8. Console.WriteLine(d);  
  9.   
  10. double price = 44.95d;  
  11. d = (dynamic)price;  
  12. Console.WriteLine(d);   
Dynamic can be used with classes too. The following example defines an Author class with a dynamic method FullNameMethod. The Main method creates a dynamic type of the Author class and calls its dynamic method by passing a dynamic parameter. 
  1. namespace DynamicProgSample  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             dynamic lastName = "Chand";  
  8.             dynamic dynAuthor = new Author();  
  9.             Console.WriteLine(dynAuthor.FullNameMethod(lastName));  
  10.   
  11.             Console.ReadKey();  
  12.         }         
  13.     }  
  14.   
  15.     public class Author  
  16.     {  
  17.         // Define a dynamic field.   
  18.         dynamic name;  
  19.         // Define a dynamic property.  
  20.         dynamic NameProperty { getset; }  
  21.         // Define a dynamic method with a dynamic parameter type.   
  22.         public dynamic FullNameMethod(dynamic d)  
  23.         {  
  24.             name = "Mahesh";  
  25.             dynamic firstname = name;              
  26.             return firstname + " " + d;  
  27.         }  
  28.     }  
  29. }  
Summary
Dynamic programming has added a new dimension to the C# language. It plays a vital role in key technologies such as LINQ. In this article, we learned about the dynamic keyword and how to define dynamic variables in the C# language. 

No comments:

Post a Comment