C# Dynamic

WBOY
Release: 2024-09-03 15:32:36
Original
572 people have browsed it

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;
Copy after login

Working of Dynamic type in C#

  • The dynamic type was introduced with the release of C# 4.0. Type checking during compile time can be avoided by using Dynamic type.
  • The type is checked by the compiler during runtime.
  • The behavior of dynamic type is like the behavior of object types.
  • GetType() method can be used to get the actual type of the dynamic variable.
  • The type of the dynamic type is changed during the runtime depending on the value on the right-hand side.
  • When a class object is assigned to the dynamic type, the correct method, and the name of the property holding the class object is not checked by the compiler.
  • A dynamic type parameter can also be passed to a method so that any type of parameter can be accepted by the method during run time.
  • An exception is thrown by the compiler during run time if the properties and the methods are not found to be compatible.
  • An error is not thrown by the compiler on dynamic type during compile time as there is no checking of type for dynamic types by the compiler during compile time.

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());
}
}
Copy after login

Output:

C# Dynamic

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.

Examples

Here are the following examples mention below:

Example #1

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");
}
}
Copy after login

Output:

C# Dynamic

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.

Example #2

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);
}
}
}
Copy after login

Output:

C# Dynamic

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:

  • Communication with other dynamic languages becomes simpler by using dynamic type in C#.
  • The responses from Application Programming Interface calls can be simplified in C# when we are not aware of what object type to expect.
  • Libraries can be created which can be used between several languages using dynamic type in C#.
  • If the main concern is not the speed, generic solutions can be made using dynamic in C#.
  • The reflection code can be replaced and made simpler using dynamic.

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!