Search This Blog

Friday 4 December 2015

var vs dynamic keyword in c#

Var Keyword


1.       Var keyword provides compile time initialization.
2.       Once you can initialize the type of value, you can’t change it.

Example

var strA = 10;
strA = "Hello";

Here, strA value assigned 10 i.e. strA containing an integer value. If you try to assigned the string value to strA variable you will get the following error.
Cannot implicitly convert type 'string' to 'int'

Dynamic keyword


1.       Dynamic keyword provides runtime initialization.
2.       You can initialize the type of value at runtime.
3.       You can re- initialize the dynamic variable.

Example

dynamic strA = 10;
strA = "Hello";

Here, strA value assigned 10 i.e. strA containing an integer value. If you change the value as string, dynamic variable behave as a string variable at runtime.

No comments:

Post a Comment