Home > Article > Backend Development > C# Dynamic
To avoid checking during compile time a new type was introduced with the release of C# 4.0 called dynamic type. Basically, a type is assigned based on the expression value by the compiler whereas this dynamic type does not undergo type check during the compile time, that is it escapes the checking of type during compile time instead the type is resolved during the run time and the dynamic keyword is used to define a dynamic type and the dynamic types are compiled into object types by the compiler and the actual type of dynamic is resolved during runtime.
Syntax:
dynamic variable_name;
Consider the example below to explain the usage of Dynamic type in a C# program:
using System; //a class called program is defined class program { //main method is called static public void Main() { // Dynamic variables are declared dynamic val1 = 1234; dynamic val2 = 1234.40; dynamic val3 = false; dynamic val4 = "Hello"; //GetType() method is used to obtain the actual type of the dynamic variables used earlier Console.WriteLine("The actual type of val1 is: {0}", val1.GetType().ToString()); Console.WriteLine("The actual type of val2 is: {0}", val2.GetType().ToString()); Console.WriteLine("The actual type of val3 is: {0}", val3.GetType().ToString()); Console.WriteLine("The actual type of val4 is: {0}", val4.GetType().ToString()); } }
Output:
In the above program, the program is the class defined. Then the main method is called. Then the dynamic variables are declared whose actual types are not known. Then GetType() method is used to obtain the actual type of the dynamic variables used earlier. The output of the program is as shown in the snapshot above.
Here are the following examples mention below:
C# program to demonstrate the use of dynamic type parameters that can be passed to a method
Code:
using System; //a class called program is defined class program { // a method called add is defined to which dynamic type variables are passed as parameters public static void add(dynamic r1, dynamic r2) { Console.WriteLine(r1 + r2); } // Main method is called static public void Main() { //add method is called to add whatever is passed as parameters to the method since the method accepts dynamic type variables add("H", "I"); add("Welcome to", " dynamic type"); add(20, 20); add(20.5, 1.5); add(100, "fun"); } }
Output:
In the above program, the program is the class defined. Then a method called add is defined to which dynamic type variables are passed as parameters whose actual type is not known. Then the main method is called. Then the add method is called to add whatever is passed as parameters to the method since the method accepts dynamic type variables The output of the program is as shown in the snapshot above.
C# program to demonstrate the use of dynamic type parameters that can be passed to a method:
Code:
using System; //a namespace called example is defined namespace example { //a class called program is defined class Program { //main method is called static void Main(string[] args) { //different types of values are passed as a parameter to GetDetails method GetDet("Learning is fun and welcome to learning dynamic type in C#"); GetDet(false); GetDet(100.22); GetDet(20); Console.ReadLine(); } //a method called getdetails is defined to which dynamic type variable is passed as a parameter so it accepts any type of parameter static void GetDet(dynamic r) { Console.WriteLine(r); } } }
Output:
In the above program, a namespace called example is defined. Then the program is the class defined. Then the main method is called. Then a method GetDet is called which is defined later, to which dynamic type variables are passed as parameters whose actual type is not known. Then the GetDet method is defined to which dynamic type variable is passed as a parameter so it accepts any type of parameter. The output of the program is as shown in the snapshot above.
There are several advantages of using dynamic types. They are:
In this tutorial, we understand the concept of Dynamic type in C# through definition, the syntax of Dynamic type in C#, working of Dynamic type in C# through programming examples and their outputs, and the advantages of using dynamic type in C#.
The above is the detailed content of C# Dynamic. For more information, please follow other related articles on the PHP Chinese website!