Difference between Var and Dynamics in C#



Var is strictly typed in C#, whereas dynamic is not strictly typed.

Var declaration

var a = 10;

Dynamic declaration

dynamic a = 10;

A Var is an implicitly typed variable, but it will not bypass the compile time errors.

Example of var in C#

var a = 10;
a = "Demo"; //  gives compile error

Example of dynamics in C#

dynamic a = 10;
a = "Demo";  // won’t give error
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements