Var Keyword in C#

In .Net, when you simply declare a variable with datatype int, it is explicit declaration

  1. int i = 0; //explicit  
But when declared with var, compiler looks for the data assigned during declaration and accordingly assign appropriate datatype to it during compilation process.
  1. var v = 0; //implicit  
For example, In above declaration, we are assigning numeric value 0 to var, so var would be replaced to int during the generation of IL Code. var define datatype statically not on run-time. In other words, they are not like objects which can once point to int, then string on run-time. variable declared with var is initialized within the declaration statement and can't be initialized with null.

Why use var?

The most important aspect of var in existence is LINQ with Anonymous Type. Using var makes your code very simple and short.

Let's first take an example code of LINQ with Anonymous Type without using var.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. namespace ConsoleApplication1  
  5. {  
  6.     class Details  
  7.     {  
  8.         public int Length;  
  9.         public string Value;  
  10.     }  
  11.     class LINQ  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             string[] Names = {  
  16.                 "Sandeep""Abhay""Ritesh"  
  17.             };  
  18.             IEnumerable < Details > details = from x in Names select new Details  
  19.             {  
  20.                 Length = x.Length, Value = x  
  21.             };  
  22.             foreach(Details d in details)  
  23.             {  
  24.                 Console.Write(string.Format("Name : {0}, Length : {1}\n", d.Value, d.Length));  
  25.             }  
  26.             System.Threading.Thread.Sleep(2000);  
  27.         }  
  28.     }  
  29. }  
In the above code, we have created a strongly typed object which we can get from a LINQ query. But, the problem is that we have written a lot of code, created a class, then put the class in IEnumerable and then we am getting property. Now using the var the same can be achieved simply with less code. The biggest advantage of using var with LINQ and Anonymous time is that we do not need to create class Details anymore.

Example of LINQ with Anonymous Type using var.
  1. using System.Collections.Generic;    
  2. using System.Linq;    
  3. namespace ConsoleApplication1    
  4. {    
  5.     class LINQ    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             string[] Names = {    
  10.                 "Sandeep""Abhay""Ritesh"    
  11.             };    
  12.             var v = from x in Names select new    
  13.             {    
  14.                 Length = x.Length, Value = x    
  15.             };    
  16.             foreach(var d in v)    
  17.             {    
  18.                 Console.Write(string.Format("Name : {0}, Length : {1}\n", d.Value, d.Length));    
  19.             }    
  20.             System.Threading.Thread.Sleep(2000);    
  21.         }    
  22.     }    
  23. }    
Output

output
Ebook Download
View all
Learn
View all