Var And Dynamic In C#

Before proceeding to understand var and dynamic keyword, let us first understand the categories of programming languages.

There are basically two categories of programming language which are given below

  • Static Type language Category
  • Dynamic language category

Static Type language

These are also called Strongly-typed language. These are the languages which perform type check at compile type. Let’s take an example. You are aware of the return type of method, however, if it is very complex then you will use var. However when you are aware of type then you directly use that datatype to declare variable like string str, int a, etc.

Let's understand var keyword in C#.

var keyword isan indirect way of defining data type.

When you define  var of specific type i.e.:

  1. string str = "Test";  

Then it is called an explicit type of declaration, it is also called a direct declaration.

Now when you define a variable using var keyword

  1. var str = "Test";  

Then it is called as implicit type of declaration or indirect type of declaration.

Now in this, compiler verifies data present on the right hand side and creates the appropriate type during the compilation process, however in the above case, compiler will check “Test” and will replace var keyword with string while generating IL code.

Var keyword defines data type statically at compile time, not at runtime, i.e.; once data type is defined it will not be changed at runtime.

Let’s see this in  the below example,

  1. var str = "Test";  
  2.           str = 123;  

Now build it, you will get compilation error for implicit conversion for str=123 as shown in the below image,

C#

Now if you will move the mouse over str on fist line then you will get (local variable string str) as shown in below image,

C#

You can verify the same by opening this exe/dll using ILDasm.exe

When to use var

  • For simple datatypes like int , double string , etc., you must always use specific data type for declaring variables , since it looks simple
  • When you have created a class with a really big name then for creating object you must use var key word.
  • When you are working with LINQ or Anonymous types then you must use var keyword.

Dynamic Type language

These are the languages which perform type check at runtime. Let's see an example. If you are not aware  of the type of value which you will get or need to be assigned, in this case type is defined at runtime.

Let's understand dynamic keyword in C#,

Dynamic keyword helps to get the benefit of dynamically static types. Let's see in the below example.

Example

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         dynamic str = "Test";  
  4.     }  

Now if you will type str., then you will not get any intellisense as shown in the below image.

C#

Now it will be resolved at runtime.

Now , after assigning value to str, if you will do some mathematical operation it will not give any error as shown in the below example.

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         dynamic str = "Test";  
  4.         str++;  
  5.     }  
  6. }  

Now, if you will build the application, the build will succeed:

C#

However if you will run it, it will give you runtime error as shown in below,

C#

Note
Dynamic keyword uses reflection internally.

Now when to use Dynamic keyword?

  • When you are using MS-office componenets inside our application
  • If you do not know the return type of a method and method is public then use Dynamic keyword.
  • If you want to perform caching then use Dynamic keyword.

One of the major differences between var and dynamic keyword is var is bound early and dynamic is bound late.


Similar Articles