- var name = "C# Corner"; // Implicitly typed.
- string name = "C# Corner"; // Explicitly typed.
- 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.
- using System;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var varInt = 1;
- var varChar = 'a';
- var varString = "abcdef";
- Console.WriteLine("{0}\n{1}\n{2}", varInt, varChar, varString);
- Console.Read();
- }
- }
- }
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.
- using System;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var aInt = new int[] { 1, 2, 3, 4, 5, 6 };
- foreach (var item in aInt)
- {
- Console.Write(item.ToString());
- }
- Console.Read();
- }
- }
- }
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.
- using System;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList list = new ArrayList();
- list.Add(1);
- list.Add(2);
- list.Add("Three");
- list.Add("Four");
- Console.WriteLine("Tong so phan Tu: {0}\n", list.Count);
- foreach(var a in list)
- {
- Console.WriteLine(a);
- }
- Console.Read();
- }
- }
- }
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.
- using System;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array = { 1, 2, 4, 6, 8, 9, 11, 14, 15 };
- //Lay cac gia tri la so nguyen chan trong mang
- var a = from i in array where i % 2 == 0 select i;
- foreach(var _a in a)
- {
- Console.WriteLine("{0}\n",_a);
- }
- Console.Read();
- }
- }
- }

Thrainder KumarPosted Aug 31, 2021, 11:40 AM
If we try to assign a string value from string list into a var variable then how to handle if where condition on list doesn't return any value.
Rajanikant HawaldarPosted Feb 28, 2020, 2:57 AM
If you add comments for code that should be in English.
Hemalatha SunnapuPosted Nov 2, 2018, 2:32 AM
Why it is allowed in method scope...any sepecific reason...
Alan MPosted Aug 10, 2018, 9:04 AM
The use of the var keyword in anything larger than a update script or a data tool is bad practice. It is bad practice for a myriad of reasons, the most generally applicable are as follows. Anything that reduces the readability of code, or introduces ambiguity on ANY level is a problem waiting to happen. Code is hierarchical by nature, meaning clear design is imperative. If you use anything that makes it unclear you are going to have design issue that will be extremely difficult to isolate. I have seen this cost thousands of euro in a previous project. Secondly, if you actually cannot understand the type that is being returned from some function or query, the query needs to be refactored. Always fit the data structure to the existing code, rather than mashing the code into dealing with fields/members that were patched in in a 17:00 on a Friday commit. Finally, if you want the code standards of your project to be adhered to, keep vars,ternary operators and bizarre RegEX to a minimum. Frustration at ambiguity will most definitely breed further ambiguity, even among senior developers. I could go on, really I could, I have seen this so many times and seen its fallout so many times more. So, take it from me, write your code properly, or your project is going to fail. Computers are supposed to be easy to use.
Mahesh ChandPosted Apr 26, 2013, 7:19 AM
You should post code as code, not images.