Var Keyword In C#

C# allows variables to be declared as explicit type or implicit type. Implicitly typed local variables are strongly typed just as if you had declared the type yourself, but the compiler determines the type at run time depending on the value stored in them. The C# var keyword is used to declare implicit type variables in C#. 
  1. var name = "C# Corner"; // Implicitly typed.  
  2. string name = "C# Corner"; // Explicitly typed.  
  3. var age = 30; 

Characteristics of C# var keyword

C# var keyword initializes variables with var support. The data type of a var variable will be determined when assigning values to variables. The following program will automatically cast the variable. And of course, after assigning the variable value, the variable has a defined data type and cannot be replaced. In the example below we have 3 variables declared with the var keyword. In turn 3 variables are assigned values with the data types int, char and string. After assigning values to variables it can be used as ordinary variables.

  1. using System;  
  2. using System.Text;  
  3. using System.Collections;  
  4.   
  5. namespace ConsoleApplication1  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             var varInt = 1;  
  12.             var varChar = 'a';  
  13.             var varString = "abcdef";  
  14.             Console.WriteLine("{0}\n{1}\n{2}", varInt, varChar, varString);  
  15.             Console.Read();  
  16.         }  
  17.     }  
  18. }  

A few notes about C# var keyword


Do not use var to declare the data type for the property (properties) as well as the return type of the method (method) in the class.

The correct type must be used to initialize var variables to assign values to variables.

Declare the array with the var keyword in C#


We can use the var keyword to declare array data types where we don't have predefined datatypes as shown in the example below. We see here how declaring an array with a var datatype does not bother us with the type of data of each element in the array. Just use a foreach loop with one var variable to find all the elements in the array.
  1. using System;  
  2. using System.Text;  
  3. using System.Collections;  
  4.   
  5. namespace ConsoleApplication1  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             var aInt = new int[] { 1, 2, 3, 4, 5, 6 };  
  12.             foreach (var item in aInt)  
  13.             {  
  14.                 Console.Write(item.ToString());  
  15.             }  
  16.             Console.Read();  
  17.         }  
  18.     }  

Var application in the treatment of ArrayList


The example below shows the advantage of var when no predefined data types for variables are known. Initially we created one list and in turn assign the data as 1, 2, Three, Four. Then use a foreach loop to browse the elements in the list, but this list is initialized with two types of values, hence it is difficult to browse the list. But if you use the var then everything becomes very easy, because no matter what the data type is, the program will automatically cast.
  1. using System;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Collections;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             ArrayList list = new ArrayList();  
  13.             list.Add(1);  
  14.             list.Add(2);  
  15.             list.Add("Three");  
  16.             list.Add("Four");  
  17.             Console.WriteLine("Tong so phan Tu: {0}\n", list.Count);  
  18.             foreach(var a in list)  
  19.             {  
  20.                 Console.WriteLine(a);  
  21.             }  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. }

Using of C# var keyword in LINQ


In the example below we see great applications of var in LINQ queries. Only one variable var can store all values returned from the query and then use a foreach (var) to retrieve the value.
  1. using System;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Collections;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int[] array = { 1, 2, 4, 6, 8, 9, 11, 14, 15 };  
  13.             //Lay cac gia tri la so nguyen chan trong mang  
  14.             var a = from i in array where i % 2 == 0 select i;  
  15.   
  16.             foreach(var _a in a)  
  17.             {  
  18.                 Console.WriteLine("{0}\n",_a);  
  19.             }  
  20.             Console.Read();  
  21.         }  
  22.     }  
  23. }  
 

When to use a var in C#

 
Don’t use var for simple local variable types that are known to you. Use of var when you’re not sure what type of data will be stored in a variable.
 

Conclusion

 
The var was introduced to C# with LINQ that uses anonmous types, query expressions, and lambdas. Use of var is useful when the type of a variable is unknown. 


Similar Articles