C# Dynamic Binding

C# dynamic is a keyword that is used to make a property or a method dynamic. When we make dynamic type, compiler does not check it at compile-time. Compiler checks it only at run time.

The purpose of using dynamic binding is to avoid compile time checking of the code.

The property created using dynamic keyword works like object. Dynamic variables are compiled into type object variables and exist only at compile time, not at run time.

The type of dynamic and object both are similar. We can check it by using the following code.

C# Dynamic Binding Example 1

Output

System.Int32
System.Int32

Now, let's manipulate the both objects and see the working difference.

C# Dynamic Example 2

Now, it produces a compile-time error due to object v1. The dynamic object does not check at compile time so, it does not produce any error at compiler-time.

Output

DynamicExample.cs(13,18): error CS0019: Operator '+' cannot be applied to operands of type 'object' and 'int'

Dynamic Properties and Methods

In the following example, we are creating dynamic properties and methods.

C# Dynamic Binding Example

Output

Peter
Welcome to the javatpoint





Latest Courses