Jan 3, 2014

Var vs Dynamic Keywords in C#


The keyword VAR was introduced in C#3.0 and the keyword Dynamic was introduced in C# 4.0.

The key differences are:
  1. The Var declarations are resolved at compile-time and the Dynamic declarations are resolved at run-time,
  2. The Var type of variable declared is decided by the compiler at compile time (Statically typed) and the dynamic type of variable declared is decided by the compiler at runtime time (Dynamically typed).
  3. The Var need to initialize at the time of declaration. Example: var str = ”Krishna”, looking at the value assigned the compiler treats the value as string; whereas for the dynamic, no need to initialize at the time of declaration. Example: dynamic str; 
  4. The Var errors are found at compile time as the compiler knows about the type and methods at compile time whereas the dynamic errors are found at runtime as the compiler knows about the type and methods at the runtime.
  5. for Var intellisense is available whereas for dynamic intellisense is not available.

No comments:

Post a Comment