difference between var and dynamic c#

 

Programming languages can normally be considered to be either statically typed or dynamically typed. A static (not to be confused with the static keyword, used for classes) typed language validates the syntax or checks for any errors during the compilation of the code. On the other hand, dynamically typed languages validate the syntax or checks for errors only at run time. For example, C# and Java are a static type and JavaScript is a dynamically typed language.


C# was previously considered to be a statically typed language, since all the code written was validated at the compile time itself. But, with the introduction of the dynamic keyword in C# 4.0, it became a dynamic typed language also. The two concepts of static and dynamic types in C# can be illustrated with the use of the two keywords named var and dynamic. Let's discuss some basic differences between these two, based on some of their characteristics.

When were they introduced

  • var was introduced in C# 3.0
  • dynamic was introduced in C# 4.0

Type inference of variables

  • var is a statically typed variable. It results in a strongly typed variable, in other words the data type of these variables are inferred at compile time. This is done based on the type of value that these variables are initialized with.
  • dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time in contrast to var type.

Initialization of variables

  • var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
  • dynamic type variables need not be initialized when declared.

Changing type of value assigned

  • var does not allow the type of value assigned to be changed after it is assigned to. This means that if we assign an integer value to a var then we cannot assign a string value to it. This is because, on assigning the integer value, it will be treated as an integer type thereafter. So thereafter no other type of value cannot be assigned. For example, the following code will give a compile time error:

 

Var

Dynamic

Introduced in C# 3.0

Introduced in C# 4.0

Statically typed – This means the type of variable declared is decided by the compiler at compile time.

Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.

Need to initialize at the time of declaration.

e.g., varstr=”I am a string”;

Looking at the value assigned to the variable str, the compiler will treat the variable str as string.

No need to initialize at the time of declaration.

e.g., dynamic str; 

str=”I am a string”; //Works fine and compiles

str=2; //Works fine and compiles

Errors are caught at compile time.

Since the compiler knows about the type and the methods and properties of the type at the compile time itself

Errors are caught at runtime 

Since the compiler comes to about the type and the methods and properties of the type at the run time.

Visual Studio shows intellisense since the type of variable assigned is known to compiler.

Intellisense is not available since the type and its related methods and properties can be known at run time only

 

e.g., var obj1;

will  throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.

e.g., dynamic obj1; 

will compile;

e.g. var obj1=1;

will compile 

var obj1=” I am a string”;

will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.

e.g. dynamic obj1=1;

will compile and run 

dynamic obj1=” I am a string”; 

will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.

This code will work

Comments

Popular posts from this blog

data annotation attributes c#

how to create windows service setup in c#.net