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.:
- 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
- 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,
- var str = "Test";
- str = 123;
Now build it, you will get compilation error for implicit conversion for str=123 as shown in the below image,

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,





Viknaraj ManogararajahPosted Jul 21, 2018, 11:04 PM
Nice Article, Thank you for sharing
Ramesh PalanivelPosted May 1, 2018, 11:53 PM
Nice One.. Great!...
S SSPosted May 1, 2018, 7:40 AM
Hi Pradeep, Can you please help with c# code to verify an “ image icon” ( ua control in listview) column. Thanks !