Dynamic Data Type

muralikrishnasurap-12704
Posted by muralikrishnasurap-12704 under C# category on | Points: 40 | Views : 997
Dynamic types were introduced in C# 4.0,Dynamic types were declared with dynamic key word,Dynamic types can accept any data value either int (or) string and also no type check performed at complete time.

But while using dynamic types make sure proper conversin happesn else we may end up with exception as show in below program.


namespace DynamicDataType
{
class Program
{
static void Main(string[] args)
{
dynamicDataType _obj = new dynamicDataType();
Console.WriteLine(_obj.GetdynamicVariableSquare());
Console.WriteLine(_obj.GetDynamicVariableData());
Console.Read();
}
}

class dynamicDataType
{

public dynamic value = 6;

public int GetdynamicVariableSquare()
{
return value * value;
}
public string GetDynamicVariableData()//Throws exception only on Run time
{ //But program compiles successfully.
return value;
}
}
}

Comments or Responses

Login to post response