Data types play very powerful role in programming, we use data type to specify type of data to be stored in variables. But what if we don’t know the type of values that is to be stored in variable in such scenario
var keyword used.  

In C# programming variables can be declared as explicit type or implicit type. Implicitly typed local variables are strongly typed just as we declared the typed variable, but the compiler determines the type at run time depending on the value stored in it . 

The C# var keyword is used to declare implicit type variables in C#. 


Characteristic of var keyword -

  • The keyword var is used to declare implicit type variables in C#, which means it tells the compiler to figure out the variable type at compilation time.
  • A variable with var keyword must be assigned a value at the time of variable declaration.
   

 Here you can see that name variable has assigned a string     “Kaushal Rao”. So now type of     variable name is string.

  • A var variable must be initialized at the time of declaration. Means we need to supply value at the time of declaration – var x=90. If you not assign a value to the variable it will be an error in the program.
  •  In C# var is strongly typed. When assigning the variable value, the variable has a defined data type and cannot be replaced, means once we assigned value to a var variable the type of variable has been defined by the type of value stored in the variable such as integer, character, float, double etc.
  • var requires fewer code changes when the return type of a method call changes. You only need to change the declaration of the method, not everywhere the method is used.
  • C# supports the variable type var since version 3.0.

Featured Post

When to use var keyword

Use of “var” is not recommended everywhere. The var was created to handle declarations when the type is not known, such as generic types, lambdas, and query expressions. If you already know the type of a variable, you must declare that explicitly.

Remember, if you don’t declare a variable explicitly, the compiler must do extra work to determine the type. While the cost of this operation may not be significant, it’s just unnecessary burden on the compiler.

We can use var in following requirements-

  • Use of var when you’re not sure what type of data will be stored in a variable.
  • Use in anonymous types and anonymous collections.
  • Use of var improves code readability. Use when class names are extremely long.
  • Imported unmanaged code types that doesn’t follow naming conventions. 

Restrictions on implicit type variables

The following restrictions apply to implicitly-typed variable declarations:

  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group,or an anonymous function.

  • var cannot be used on fields at class scope.

  • Variables declared by using var cannot be used in the initialization expression.

How to use var keyword

				
					using System;  
using System.Text;  
using System.Collections;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var intVar = 1;  
            var charVar = 'a';  
            var strVar = "use of var keyword";  
            Console.WriteLine("{0}\n{1}\n{2}", intVar, charVar, strVar);  
            Console.Read();  
        }  
    }  
}  
				
			

Array with the var keyword in C#

We can also create an array with var if we dont know the type of value to be stored in an array.

				
					using System;  
using System.Text;  
using System.Collections;  
  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            var arr = new int[] { 10, 20, 30, 40, 50, 60 };  
            foreach (var item in arr)  
            {  
                Console.Write(item.ToString());  
            }  
            Console.Read();  
        }  
    }  
} 
				
			

Add Your Heading Text Here

 “var” is used to store anonymous types. The code snippet in creates and stores an anonymous type.

				
					// author is an anonymous type  
var author = new { 
                  Name = "Kaushal Rao", 
                  Book = "Programming  in C#", 
                  Year = 2023, 
                  Price = 230
                 };  
 
Console.WriteLine("Author details: {0}, {1}, {2}, {3}", author.Name, author.Book, author.Year, author.Price); 
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *