find and solve ||
Please wait.....

Dynamic in C#

Dynamic and Static Type 

All types of OOP programming languages can normally be considered to be either dynamically typed or statically.A static keyword used for classes which is alidates the syntax or checks for any errors during the compilation of the code.And Dynamically keyword or languages validate the syntax or checks for errors only at run time.For Example JavaScript is a dynamically language and C# and Java are a static language.
C# was considered to be a statically typed language, all the code arevwritten valid at the compile time itself.The  dynamic keyword was introduction  in C# 4.0,it became a dynamic typed language also.There are two types of concept dynamic and static types in c# which are given belwo. ⦁    var was introduced in C# 3.0
⦁dynamic was introduced in C# 4.0

Type inference of variables 
 var is a statically  variable which is it results in a strongly typed variable, The data type of these variables are inferred at compile time. This is  a based on the type of value that these variables are initialized with.
⦁ dynamic are dynamically  variables type. That  means, their type is inferred at run-time and not the compile time in contrast to var type.

Initialization of variables  
 ⦁var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
⦁The dynamic type variables need not be initialized when declared. 
Example: dynamic type variable
dynamic dynamicVeriableExample = 1;
In most cases,the compiler compiles dynamic types into object types.The given above statement would be compiled as:
dynamic type at compile time
object dynamicVeriableExample = 1; The dynamic would resolve at a runtime.
 You can check the type of the dynamic variable, as given belwo below:
  For Example: Get the actual type of dynamic type at runtime
     static void Main(string[] args)
            dynamic dynamicVariableExample = 1;

In the run time,a dynamic type changes its type based on the value of the expression to the right of the "=" operator. In the given below example how to dynamic variable changes its type based on its value:  
static void Main(string[] args)
            dynamic dynamicVariableExample = 200;
            Console.WriteLine("Dynamic variable value: {0}, Type: {1}"
            dynamicVariableExample, dynamicVariableExample.GetType().ToString());          
            dynamicVariableExample = "Hello World!!";             Console.WriteLine("Dynamic variable value: {0}, Type: {1}",
            dynamicVariableExample, dynamicVariableExample.GetType().ToString());                                       
            dynamicVariableExample = true;             Console.WriteLine("Dynamic variable value: {0}, Type: {1}"
            dynamicVariableExample, dynamicVariableExample.GetType().ToString());  
            dynamicVariableExample = DateTime.Now;             Console.WriteLine("Dynamic variable value: {0}, Type: {1}"
            dynamicVariableExample, dynamicVariableExample.GetType().ToString());         }
Dynamic variable value: 200, Type: System.Int32
Dynamic variable value: Hello World!!, Type: System.String
Dynamic variable value: True, Type: System.Boolean
Dynamic variable value: 01-01-2019, Type: System.DateTime

Methods and Properties of Dynamic Type  If We assign a class object to the dynamic type then the compiler would not check it for correct methods and properties name of a dynamic type that holds the custom class object.Consider the given below  example.

Example: dynamic 
 public class Staff {
            public int StaffId { getset; }
            public string Name { getset; }
            public int Age { getset; }

            public void DisplayStaffDetail()             {                 Console.WriteLine("Name: {0}", this.Name);                 Console.WriteLine("Age: {0}", this.Age);                              }         }         
    class Program         {             static void Main(string[] args)             {                 dynamic dynamicStaff = new Staff();           
                dynamicStaff.FakeMethod();             }         } 
In the given above example, the Staff object assigned t to a dynamic can see the  second statement in Main() method, we call FakeMethod() method, which is not exists in the Staff class.But the compiler will not give any types of  error for FakeMethod() because it skips type checking for dynamic type, instead you will get a runtime exception.  
Accepted Dynamic Type as a Method Parameter  
class Program
            static void CheckValue(dynamic val)

  static void Main(string[] args)             {                
                CheckValue("Hello example.");                 CheckValue(100);                 CheckValue(100.50);                 CheckValue(true);                 CheckValue(DateTime.Now);             }         }
Hello example.
01-01-2019 10:10:50
Points to Remember :  ⦁    
Resolved at runtime instead of compile time.
⦁    Compiler skips the type checking for dynamic type. So it doesn't give any types of error about dynamic types at compile time.
⦁    Do not have intellisense support in visual studio.
⦁    it's  method can have parameters of the dynamic type.
⦁    An exception will be thrown at runtime if a method or property is not compatible.

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If felt valuable to you, feel free to share it.


Report Response