Var and Dynamic Keywords in C#
The var and dynamic types in C#:
Primitive Datatypes :
- Generally we use the datatype to declare and store the value inside the variable but in the C#, we have another 2 ways to do the same that is using the
varanddynamickeywords we will discuss these keywords in later parts of this blog. ###datatype variableName = value; //this is declaration of variable using the primitive data types int a = 10;vartype : The
vartype is introduced in the C# 3.0 version. Thevarare those variables that are declared without specifying the type explicitly. Instead, we use thevarkeyword to declare the variables. The type check occurs during the compilation. These are not designed to replace the normal variable declaration, it is designed to handle the special case situation.- Difference between the implicitly typed and explicitly typed
int i = 10; // Explicitly type of the variable is mentioned. var i = 10; // Implicitly type check occurs by the compilerdynamictype :
- Difference between the implicitly typed and explicitly typed
- The
dynamictype is introduced in the C# 4.0 version. Here also we don't need to mention the type of variable like thevar. Here we can declare the variable using thedynamickeyword. The compiler will not check the type of the variable which is declared using thedynamickeyword during compile time. This is used to avoid the compile-time type checking.- Difference between the implicitly typed and explicitly typed
int num = 8; // Explicitly type of variable is mentioned. dynamic = 8; //Implicitly type check occurs during runtime
- Difference between the implicitly typed and explicitly typed
