Dynamic Data Type in C#

What is a Dynamic Data Type?

Well, it was introduced with C# 4.0, with its help we can assign the data in a variable at runtime. We can define this data type by using the keyword “dynamic" in our code. A variable of the dynamic data type can be initialized by any type of data, such as the numbers, float values or the string data. The following example will demonstrate the use of the dynamic data types:

Let's create a console application (ctrl+alt+n) named DynamicDataType,

And try this:-

Dynamic Data Type Class

Understanding the Dynamic Data Type

All the member functions and the member variables, such as the fields, methods, properties, that are associated with the dynamic keyword are ignored at the compile time, the compiler preserves all the information about the expression, such as the data types, and uses it later to evaluate the expression at the runtime.

The dynamic data types act similar to the var data type.

The difference between var and dynamic is that the var is strongly type checked at the compile time; whereas, the ddt (dynamic data type) is type checked by the compiler only at runtime.

After declaring a var data type, we cannot explicitly change its type throughout the execution of the program.

Thank you!!