Dynamic Type
C# 4.0 (.NET 4.5) introduced a new type that avoids
compile time type checking. Dynamic Type
means that you can store any type of value in the dynamic data type variable because type
checking for dynamic types of variables takes place at run-time.
Dynamic Type Syntax
dynamic type can be defined using the dynamic keyword. A dynamic type changes its type at runtime based on the value of the expression to the right of the "=" operator. The following example shows how a dynamic variable changes its type based on its value.
dynamic dynamicVariable = 1;
dynamicVariable=“Hello world”;
Important Points
In most of the
cases, the dynamic type behaves like object types.
You can get
the actual type of the dynamic variable at runtime by using GetType() method.
It does not
support the intellisense in visual studio.
The compiler
does not throw an error on dynamic type at compile time if there is no type
checking for dynamic type.
The type of
the variable is decided by the compiler at run time.
If the
variable does not initialized it will not throw an error.
No comments:
Post a Comment