Skip to main content

Command Palette

Search for a command to run...

Var and Dynamic Keywords in C#

Published
3 min read

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 var and dynamic keywords 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;
    

    var type :

  • The var type is introduced in the C# 3.0 version. The var are those variables that are declared without specifying the type explicitly. Instead, we use the var keyword 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 compiler
      

      dynamic type :

  • The dynamic type is introduced in the C# 4.0 version. Here also we don't need to mention the type of variable like the var. Here we can declare the variable using the dynamic keyword. The compiler will not check the type of the variable which is declared using the dynamic keyword 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
      

More from this blog

Untitled Publication

14 posts

React Developer | Frontend Developer