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();
}
}







ayeshPosted Dec 16, 2024, 3:54 AM
Amazing and very informative
SavindraSingh ShahooPosted Aug 24, 2020, 8:56 PM
Awesome. Very clear and precise.
Ajay MalikPosted May 8, 2016, 10:34 AM
nice
Santhakumar MunuswamyPosted Feb 9, 2016, 11:44 AM
Thanks for nice article
Pankaj Kumar ChoudharyPosted Feb 9, 2016, 10:36 AM
Thanks Anil Kumar For Your Kind Word. It is my pleasure that you find my article useful ....
Anil Kumar MurmuPosted Feb 9, 2016, 6:32 AM
i appreciate your efforts.
Anil Kumar MurmuPosted Feb 9, 2016, 6:32 AM
very nicely explained dynamic vs var keyword. Few of the concepts are new to me. I never looked into them in this context. For e.g using dynamic/var as return type, using as parameters to functions
Pankaj Kumar ChoudharyPosted Feb 8, 2016, 11:32 AM
Thanks Ehsan Sajjad........
Pankaj Kumar ChoudharyPosted Feb 8, 2016, 11:31 AM
Thanks Shubham Kumar.......
Pankaj Kumar ChoudharyPosted Feb 8, 2016, 11:31 AM
Thanks Vignesh Mani Sir........
Pankaj Kumar ChoudharyPosted Feb 8, 2016, 11:31 AM
Thanks Nanddeep Nachan Sir.........
Ehsan SajjadPosted Feb 8, 2016, 7:14 AM
Good one
Shubham KumarPosted Feb 8, 2016, 4:34 AM
nice
Vignesh ManiPosted Feb 8, 2016, 4:28 AM
Nice
Nanddeep NachanPosted Feb 8, 2016, 4:00 AM
Nice share