Dynamic type in C# is typically a data type , it was introduced in C#.Net 4.0 . As the name suggests its dynamic in nature and the type information will be evaluated dynamically during run time.

So, basically when we declare a dynamic type variable, the compiler doesn’t make any type check during compilation time. However during run-time it looks at the type of expression on the right side of the assignment operator (=) and determines actual type.

Syntax & Usage :

We can define dynamic type variables  using dynamic keyword.

In the above example d  is declared as dynamic , here compiler doesn’t know about the data type of d during compile time, it evaluates the type information during run time. In the above case it will resolve type of d to System.String . Lets see some more example as below.

In the above example dn is defined as a dynamic variable, Here the run-time derives the type in the first case dn to System.String type and the second case to System.Int32

Declaration and usage of dynamic type in C# example :

In below example.

  • We have declared a dynamic variable and at run time we are assigning variables of different types to it.

Output :

dynamic type out put 1.0

In above example the variable d is resolved to different data type at run time based on the assignment.

Lets look at below code example, here the code compiles successfully but throws an exception at run time.

In the above example, Here compiler doesn’t know about the type information of variable d , hence it didn’t raise any error in line  d++ during compilation. However during run time it throws an error, because compiler resolved the data type d  to System.String type and increment operation d++ is not supported on a String type.

Usage in Method and Property :

If you assign a class object to the dynamic type, then the compiler will ignore all type check during compilation time. It won’t validate for correct methods and properties name of the dynamic type that holds the class reference.

Usage of C# dynamic in method and property example:

If you look at below example,

  • We have declared a class as Employee , it has a method as public int CalculateSalary(int type)  and property as  string Name
  • In the Main method, we are creating an employee instance of type dynamic and accessing its method and property.

dynamic Type Out Put 1.1

In the above example, Employee class don’t have Salary property, hence compiler throws a run time error. Similarly compiler throws run time error while calling int Salary = dynmEmployee.CalculateSalary(); because of empty parameter and Salary = dynmEmployee.CalculateSalary("12345"); for invalid parameter. However in all cases there wont be any compile time error.

While using dynamic type it should be note that, C# compiler does not check for the parameters count, type, non-existent parameter, property type, property existence etc during compilation. It validates all these information during run-time, and if it is not valid, then throws a run-time exception.

Dynamic type vs Object type :

All types in C#.Net are derived directly or indirectly from System.Object. We can assign values of any other types, value types, reference types, predefined or user-defined types to object type.

It does not allow to perform any mathematical operation without doing boxing and unboxing. Which means, we have to perform explicit type casting upon Object type for any run-time operation.

Lets take below example.

Dynamic_Type_OutPut_1.2

Here in above, we have to cast the underlying type present in object before using it.

In above, we cast the object type to System.Int32 type.  However, using dynamic in this scenario doesn’t require any type cast, the compiler would fetch the exact type System.Int32 at run time.

Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on

Var type in C#,

Nullable type in C# ,

Anonymous type in C# and

Reference Type and Value type .