C# is the statically
typed language. What does this mean? C #checks type of the object at compile type.
A dynamic language identifies type of the object at run time; they do not
perform the compile time type checks.
C# 4.0 introduces the
dynamic language feature in C# (which is static language) using new
Dynamic keyword. Dynamic is the static type in C# and hence keep the c#
as a statically typed language. This keyword tell compiler that do not perform
type check and do not identify the type of the object.
dynamic test = 100;
//here the test will be int32
console.write(test.GetType())
// output will be "System.Int32"
dynamic test = "This
is string"; //here the test will be string
console.write(test.GetType())
// output will be "System.String"
(For more information on
C# Dynamic keyword please check references below.)
So does it look similar
to what System.Object and var keyword perform? So let us look at the difference
between them
System.Object
System.Object is the root
in the C# class hierarchy; this is used when the there is no or little
information on the identity of the object at a compile time.
Object myObject = 100;
console.write(myObject
.GetType()) // This will print System.Int32
Above statement will
print System.Int32
We need to cast the
variable myObject to specific type before using it
As shown below
int sum = (int)myObject +
10; //myObject is first converted to int before adding 10 to it.
We can assign different
values to the myObject at a run time without any problem as shown below
var keyword
A var is an implicitly
typed variable .When the variable is declared as var then its type is inferred
from the initialization string at compile time. Once initialized; type of the variable
cannot be changed at a run time.
var variables cannot be
used as function argument and cannot be returned from the function; Whereas
object and dynamic variables can be used as a function argument and can be
returned from the function.
var testvar = 100;
console.write(testvar
.GetType()) // This will print System.Int32
Above statement will
print System.Int32
No explicit cast is
required before using the testvar variable.
int sum = testvar + 10; //No cast is required.
Once initialized type of
the variable cannot be changed at a run time
testvar = "test
string"; //This will give compile time error
Above statement will give
compile time error and hence the type of the variable is checked at a compile
time
Dynamic
In case of dynamic variables
type is identify only at run time
dynamic test = 100;
//here the test will be int32
console.write(test.GetType())
// out put will be "System.Int32"
No cast is required like
in case of System.Objcet while using it
int sum = test + 10; //No
cast required here
We can assign change the
type of values assigned at a run time like
test = "testing
string"; //It works fine with dynamic
When to use the dynamic
So now we know the difference
between object, var and dynamic; now let us see when to use dynamic.
We can use the object in
all the cases where we can use the dynamics but in that case we need explicit
casts and there is overhead of boxing and un-boxing. In case of dynamic type of
the object is resolved at run time and no casting is required. This fits best
choice to use in case of reflection and working with COM interop of MS office.
References and further
study
No comments:
Post a Comment