C# Dynamic Data Type
Dynamic data type was introduced with C# 4.0. Dynamic data types are dynamic in nature and don’t require initialization at the time of declaration. It also means that a dynamic type does not have a predefined type and can be used to store any type of data.
We can define this data type using the keyword “dynamic" in our code. A variable of the dynamic data type can be initialized by any type of data like int, float ,string or object. Dynamic variables can be used to create properties and return values from a method.
Example
class Demo
{
dynamic Data = 12;
public int Method(int A, int B)
{
return (A + B) * Data;
}
}
class Program
{
static void Main(string[] args)
{
Demo Obj = new Demo();
dynamic value1 = 10;
dynamic value2 = 11;
dynamic Str = "Your Result Is: ";
Console.WriteLine(Str + Obj.Method(value1, value2));
Console.ReadLine();
}
}
Output
Expression for dynamic data types is evaluated at run time, it means all the member functions and the member variables that are associated with dynamic keywords 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.
Example
class Demo
{
dynamic Data = 12;
public int Method(int A, int B)
{
return (A + B) * Data;
}
}
class Program
{
static void Main(string[] args)
{
// Obj as an Object of Demo Class
dynamic Obj = new Demo();
dynamic value1 = 10;
dynamic value2 = 11;
dynamic Str = "Your Result Is: ";
Console.WriteLine(Str + Obj.Method(value1, value2));
// Obj as a String
Obj = "Pankaj Kumar Choudhary";
Console.WriteLine(Obj);
// Obj as an Integer
Obj = 12345;
Console.WriteLine(Obj);
// Obj as a Float
Obj = 123.45;
Console.WriteLine(Obj);
// Obj as a Boolean
Obj = false;
Console.WriteLine(Obj);
// Obj as a Dictionary
Obj = new Dictionary<int, string>();
Obj[0] = "Haryana";
Obj[1] = "Rajasthan";
Obj[2] = "Punjab";
Obj[3] = "Goa";
foreach (var index in Obj.Keys)
{
Console.WriteLine(Obj[index]);
}
Console.ReadLine();
}
}
Output
We can change the types for a dynamic data type. In the above example we assign the int, float, string, object and types for dynamic data type(obj). Size of dynamic data type is calculated the values that is assigned for dynamic data type.
Conversion between Dynamic and Implicit data type
Conversion b/w dynamic type and implicit data type is very easy. It enables the developer to switch between dynamic and non-dynamic behavior.
Example
class Demo
{
dynamic Data = 12;
public int Method(int A, int B)
{
return (A + B) * Data;
}
}
class Program
{
static void Main(string[] args)
{
// Conversion From Implicit Type To Dynamic Type
dynamic Obj = new Demo();
dynamic value1 = 10;
dynamic value2 = 112.45;
dynamic Str = "This is a string";
// Conversion From Dynamic Type To Implicit Type
Demo dm = Obj;
int int_ = value1;
double ft = value2;
string Str_ = Str;
Console.WriteLine(Obj.Method(100, 20));
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(Str);
Console.WriteLine(dm.Method(11, 12));
Console.WriteLine(int_);
Console.WriteLine(ft);
Console.WriteLine(Str_);
Console.ReadLine();
}
}
Output
Overload Resolution with Arguments of Type Dynamic
Overload resolution occurs at run time instead of at compile time if one or more of the arguments in a method call have the type dynamic. In the below example in the main method we are passing two dynamic data types in Method. In Demo class all argument of Call method are integers but in the main method weare passing two dynamic data types in which the first dynamic type(value1) contains string value, so an error will occur at run time instead of compile time.
Example
class Demo
{
dynamic Data = 12;
public int Call(int A, int B)
{
return (A + B) * Data;
}
}
class Program
{
static void Main(string[] args)
{
dynamic Obj = new Demo();
dynamic value1 = "10";
dynamic value2 = 112;
Console.WriteLine(Obj.Call(value1, value2));
Console.ReadLine();
}
}
Output
Difference between var and dynamic keyword
The var keyword can be used to store any data type value. The var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. The var keyword was introduced in C# 3.0 and dynamic was introduced in C# 4.0. Let us consider some differences between both keywords.
Statically Typed and Dynamically Typed
This means the type of variable declared for var is decided by the compiler at compile time but the type of dynamic variable declared is decided by the compiler at runtime. So type of var variable is known at compile time so error for var variable is generated at compile time but in case of dynamic, type of variable is unknown till runtime so error is generated for dynamic time variable at run time.
Need of Initialization
Need of initialization for var is required at the time of declaration; if we don’t initialize the value we will get an error but in the case of dynamicthere is no need to initialize at the time of declaration.
Multiple Initialization with different Data Type
We can not change the data type for a var keyword. It means if we assign the integer value at time of declaration then further we can assign string, double, or other data type value for var keyword. If we try to do so then compiler will throw an error. In case of dynamic type there are no such types of restrictions, we can assign different types of value for dynamic type variable.
Function Parameter
We can’t use the var keyword as a parameter for any function but the dynamic type can be used as parameter for any function .
Return Type
We can’t create a method with var as return type but we can create a method whose return type is dynamic.
Conclusion
Dynamic Type is a nice feature to use dynamic language runtime. However, dynamic type also makes it the responsibility of the developer for the correct usage of the calls.
Read more articles on C# Programming: