C# dynamic Data Type

Updated on     Kisan Patel

The dynamic data type is a new data type that supports the late binding. This implies that all the member functions and member variables such as fields, methods and properties that are associated with dynamic keyword are ignored at compile time. The compiler preserves all the information about the expression, such as data type is similar to the var data type.

The difference between the var and dynamic data types is that var is strongly type checked at compile time; whereas, the dynamic data type is type checked by the compiler only at run time. After declaring a var data type, you cannot explicitly change its type throughout the execution of the program.

Following code shows the declaration of a dynamic data type:

dynamic intDyn = 4;
Console.WriteLine(intDyn);

dynamic floatDyn = 4.16;
Console.WriteLine(floatDyn);

dynamic stringDyn = "Hellow World!";
COnsole.WriteLine(stringDyn);

In the above code even if you replace the dynamic keyword with the var keyword, the result would be the same.

string hello = "Hello World!";
var str = hello;
str.NonExistFunction();

In the above code, the NonExistFunction() function is not declared or defined anywhere in the class, the str variable, which is of the var data type calls this method. The error generated by the compiler for the above code is shown in below screenshot:

dynamic-data-type-error-example

The error generated by the compiler indicates that the var keyword recongnizes the value, operators, method assigned to it. Now, let’s compile the same code by replacing the var keyword with the dynamic keyword, as shown in the following code:

string hello = "Hello World!";
dynamic str = hello;
str.NonExistFunction();

In the above code, the NonExistFunction() method called by the str variable, which is of the dynamic data type. In this case, the compiler does not check whether the method exists or not, as it is associated with the dynamic keyword. Therefore, the compiler does not raise any error and compiles the successfully. The dynamic data type is very useful when the programmers want to allocate tha data type of the variable at run time or to store the values generated by the program at runtime.

Note that the dynamic data type is data type that is resolved at runtime, instead of compile time.

Let’s take one example to understand how you can use dynamic data type in C#.

class Program
{
    static dynamic dyn = 4.54;
    public void Method1(string str, int i)
    {
        Console.WriteLine(i + str);
    }

    public static void Main(string[] args)
    {
        Program p = new Program();

        p.Method1("Hello", 45);

        //The following line compiles successfully. but, shows an error at run time.

        p.Method1("Hello", dyn);
        // an integer value us required as the second argument

        Console.ReadLine();
    }
}

The above C# program throws an exception at run time as shown in below screenshot:

runtime-error-dynamic-keyword

The compiler does not type check the dyn variable, which is of dynamic data type, at compile time. Therefore, the above code compiles successfully.


C#

Leave a Reply