When to use var keyword in C#?

You must be thinking that there are many data types are provided by .NET Framework then why var is required?

 

Introduction:

Var was introduced in C# 3.0 and .NET framework 3.5. C# is strongly type hence var is also strong type. Type of Var is decide at compile type when we assign value of the var type variable. It cannot be changed after that because it takes data type of assign value.

 

image

 

var i = 10 is treated same as int i =10 by compiler. Here 10 is assigned to int variable i. So type of variable i is int. Now we are assigning string “rajesh” to i. Compiler throws error. You can see in above image.

 

When to Use?

  • Var can only be defined in a method as a local variable.
  • Easy to read and write code when we use Var keyword.
  • Var is used to store reference of anonymous type.

Ex: In below code you can see. I wrote linq expression. Select statement is returning object of unknown type. Hence var key word has been used to store the result of select statement.

1 public class Program 2 { 3 4 5 public static void Main() 6 { 7 8 var names = new List<Employee>{ 9 10 new Employee{ Id = 1, Name="Rajesh", Email = "rajeshatkiit@gmail.com", ContactNo= 9989898990}, 11 new Employee{ Id = 2, Name="Ram", Email = "ram@gmail.com", ContactNo= 232323223}, 12 new Employee{ Id = 3, Name="Ramesh", Email = "ramesh@gmail.com", ContactNo= 8989898898}, 13 }; 14 15 //Var keyword is used here because select statement is returning anonymous type, 16 //Type of object which is return is not known. 17 var result = (from n in names 18 where n.Id == 2 19 select new { n.Name, n.ContactNo } 20 ); 21 22 //Var keyword is used here because result of of anonyous type. 23 foreach (var item in result) 24 { 25 Console.WriteLine("Name: {0}, ContactNo: {1}", item.Name, item.ContactNo); 26 } 27 28 Console.WriteLine(); 29 30 Console.ReadLine(); 31 } 32 }

  • Var is used in for and foreach loop.
  • Var is used in using statement.
  • Var is used in Linq expression.
  • Var is used when the object name is too long.
  • Var can be declare and initialized in single statement.

Ex:

Var  i = 100;  //Correct.

Var x; x = 10; // in correct. Not initialize in single statement as above line.

 

When Not To Use?

  • Var cannot be used on field in class.
  • Value of Var cannot be assign null.

Var x = null; // Not correct.

  • Var cannot be used in property.

Ex: You can see below image, compiler is showing error when property is declared with var keyword.

           image

 

  • Var cannot be used in method parameter.

Ex: Here I have passed var as method parameter and compiler is showing error.

          image

  • Var cannot be used as return type of method.

Ex: You can see below compiler is giving error when return type of method is var.

    image

 

 

  • Var cannot be used like var x +=100;
  • Multiple var statement cannot be declare and initialize in single statement.

Ex: var x = 100, y = 200;

Conclusion:

  • Use var when you do not know of type of object which is return from right side of var statement.
  • You should not use var when type of variable is known.

 

References:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables

 

Please provide your valuable comment and suggestion.

Thanks for reading. Happy Coding Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*