Link Search Menu Expand Document

C Sharp Dynamic Data Type

This article explains how you can use the C# Dynamic Data Type to define data variables that are initialized at runtime.

What is a Dynamic Data Type in C#

C# was initially designed as a strongly-typed language where you have to specify the type of data that a variable will store while defining a variable. For instance, if you want to store an integer value in a variable, you have to define an integer type variable. Furthermore, type-checking is performed at compile time in C#. For example, if you assign an integer value to a string type variable, the code will not compile.

However, in C# 4.0 (.NET 4.5) C# introduced the dynamic data type. The dynamic data type differs from normal data types in two ways:

  1. You do not need to specify the data type with a dynamic data type. You can simply use the keyword “dynamic” while creating a dynamic data type.
  2. The type-checking of a dynamic data type is performed at runtime which is unlike other data types whose type-checking is performed at compile time.

Defining a Dynamic Data Type

To define a dynamic data type in C#, you need to use the keyword “dynamic” followed by the variable name. Here is an example.

The following script defines a dynamic variable called “name” and assigns a string type value “hello” to it. Next, the value of the dynamic variable is printed on the console. To get the type of value assigned to a dynamic variable at runtime, you can simply call the GetType() method.

The value of the dynamic variable “name” is updated with an integer value of 10 at runtime, and the new value and its type are again displayed on the console.

class Program
{
static void Main(string[] args)
{
dynamic name = "Hello";

Console.WriteLine(name);
Console.WriteLine(name.GetType());

name = 10;

Console.WriteLine(name);
Console.WriteLine(name.GetType());

Console.ReadKey();
}
}

The above script will not throw an error at compile time since the type-checking, in this case, is not performed at compile time.

If you change the type of the “name” variable to string in the above script, the code will not compile because then you would not be able to update the value of the “name” variable using an integer. For instance, the following script will not compile:

class Program
{
static void Main(string[] args)
{
string name = "Hello";

Console.WriteLine(name);

name = 10;

Console.WriteLine(name);

Console.ReadKey();
}
}

You will see the following error if you try to run the above script:

C Sharp Dynamic Data

Using Dynamics Types with Custom Objects

If you assign an object of your custom class to a dynamic data type variable, the type checking for custom object members, and custom object method parameters is not performed at compile time. Rather the object members and the type and number of parameters passed to object methods are evaluated at runtime. Look at the following example.

class Program
{
static void Main(string[] args)
{
dynamic stu = new Student();
stu.ShowMessage("Hello from main");

stu.ShowMessage(10, 20);
stu.ShowMessage(true);

Console.ReadKey();
}

}

public class Student
{

public void ShowMessage(string message)
{
Console.WriteLine(message);
}
}

In the script above, you create a custom class i.e. Student with one method ShowMessage() that prints the string passed to it as a parameter value.

Inside the Main method of the Program class, you create an object of the Student class and assign it to a dynamic variable stu. Now when you call the ShowMessage method using the dynamic stu object of the Student class, you can pass any type and number of parameters to the function call. For instance, in the above script, there is three function calls to the ShowMessage() method. A string is passed in the first function call, followed by two integer values in the second function call, and a boolean value in the third function.

If you compile the above code, it will run without an error. At run time, the first function call will successfully execute and you will only see the error on the second function.

Now if you assign the object of the Student class to a Student class type object, you will see that the code will not compile as shown below. This is because the function calls and the number of parameters in the following script will be checked for type-safety at compile time.

class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.ShowMessage("Hello from main");

stu.ShowMessage(10, 20);
stu.ShowMessage(true);

Console.ReadKey();
}

}

Dynamic Data C Sharp

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy