System.Object
All
the objects (value and referenced types) in managed environment are directly or
indirectly inherit from
System.Object
. With this inheritance in place, a variable of System.Object
can hold
instance of any type.
However compiler
will allow to call only those methods /properties which exists in
System.Object
type.
In other words compiler will not allow
you to call CreateNode property from obj. The only way to access the
CreateNode()
is to typecast it into XmlDocument
type.
E.g. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
object obj = xDoc;
Hence:
a. Though variable of System.Object type
can hold the reference of any type, compiler will ensure the type safety. If
type casting is happening, compiler will emit the code to validate the type
safety during runtime.
b. Assigning the value type to
a variable of System.Object will suffer from Boxing / Unboxing.
Var Keyword
The
var
keyword was introduced in C# 3.0. It was
primarily introduced to support the notion of anonymous types.var
is resolved at the compile time,
i.e., the actual type is resolved by the compiler during the compile time.
Hence it provides same type safety as using normal typed variable.
Hence,
a. var is a compile time
feature resolved to a valid underlying type. Due to this, compiler enforces you
to assign a value during the declaration so it can infer the actual type.
b. Since var is
resolved to a valid type during compilation, compiler will enforce all type
safety and code will not suffer from boxing / unboxing.
Dynamic
dynamic
keyword was
introduced with C# 4.0. When you
define a variable of type dynamic, compiler internally performs two steps. The
first thing compiler does is, it converts the type to System.Object
and then it postpones all the type
validation to the runtime.
Hence
1. During compilation time, dynamic is
converted to System.Object and compiler will emit the code for type
safety during runtime.
2. As dynamic is treated as System.Object,
it suffers from boxing / unboxing similar to System.Object.
3. Since compiler emits the
code for all the type safety, application’s performance will suffer.
dynamic
was introduced only to simplify the
access to the COM APIs.
To put it as simple as possible, there is a simple difference
between the C# ‘var’ and ‘dynamic’ keyword. When using the ‘var’ keyword, the
type is decided by the compiler at compile time, whereas when using the
‘dynamic’ keyword, the type is decided by the runtime.
C# is a statically typed language and the ‘dynamic' type
automatically tells the compiler that it is a dynamic invocation, so ignore the
compile time checking for this type. This also means that any invalid
operations will only be detected at runtime.
E.g.
Consider following example
var emp = new Employee();
emp.AddEmployee();
In this case,
i.e. when you are using the ‘var’ keyword, the type is statically inferred. So
the line of code evaluates as
Employee emp = new Employee();
The
compiler will check to see if a AddEmployee () method exists, if not, fail the
compilation.
Consider following case
dynamic emp = new Employee();
emp.AddEmployee();
Using the ‘dynamic’ type does not make it
type-safe, hence the compiler is bypassed and the compiler does not check if
the AddEmployee () method exists.