C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: Please note that you cannot use an old version of the .NET Framework to compile this program.
List: This code uses the var keyword and the List type. The variable is known to be a List. So var refers to a List of ints.
ListInt, uintTip: You can hover over the var keyword and Visual Studio tells you the type List. This feature is called IntelliSense.
C# program that uses var, creates List
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// Hover over the var keyword.
// ... Visual Studio will tell us the referenced type.
var codes = new List<int> { 1, 2, 7, 9 };
Console.WriteLine("LIST COUNT: " + codes.Count);
}
}
Output
LIST COUNT: 4
Foreach: You can use var with the foreach-loop. You can use it like any other variable.
ForeachTip: The var keyword can represent any type that can be determined at compile-time. It is precisely equivalent after compilation.
C# program that uses var, query expression
using System;
using System.Linq;
class Program
{
public static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
// Use a query expression to get all the odd numbers from the array.
// ... The var stores the result IEnumerable.
var items = from item in numbers
where (item % 2 == 1)
select item;
// Each item is an int.
foreach (var item in items)
{
Console.WriteLine("VAR LOOP: {0}", item);
}
// Use the same loop but without var.
foreach (int item in items)
{
Console.WriteLine("LOOP 2: {0}", item);
}
}
}
Output
VAR LOOP: 1
VAR LOOP: 3
VAR LOOP: 5
LOOP 2: 1
LOOP 2: 3
LOOP 2: 5
C# program that causes cannot assign null error
class Program
{
static void Main()
{
var test = null;
}
}
Output
Error CS0815
Cannot assign <null> to an implicitly-typed variable
IL: You can see it with a utility called IL Disassembler provided with Visual Studio. There are two int32 values in the IL.
IL DisassemblerNote: The int declaration is the same as the var declaration in the IL. So the execution engine doesn't know that you used var.
And: They are compiled to the same IL. The var keyword is equally fast as explicit types like int or string.
ILMethod using var: C#
public int ReturnValue()
{
var a = 5;
int b = 5;
return a + b;
}
IL of the method
.method public hidebysig instance int32 ReturnValue() cil managed
{
// Code size 9 (0x9)
.maxstack 1
.locals init ([0] int32 result,
[1] int32 CS$1$0000)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: stloc.1
IL_0005: br.s IL_0007
IL_0007: ldloc.1
IL_0008: ret
} // end of method VarKW::ReturnValue
And: This simplifies the syntax of your code. It improves readability. Use it in your code.
C# program that uses var, Dictionary
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Use implicit type keyword var on Dictionary instance.
// ... Then use the collection itself.
var data = new Dictionary<string, int>();
data.Add("cat", 2);
data.Add("dog", 1);
Console.WriteLine("cat - dog = {0}",
data["cat"] - data["dog"]);
}
}
Output
cat - dog = 1
So: The C# compiler raises errors and warnings because it wants your code to work properly.
And: It makes inferences about your code and can deduce the type of a variable.
Brevity: Languages sometimes have lengthy keywords. You can reduce this with var—it's only 3 letters.
Info: Some code becomes much easier to read with this keyword. Var can even make code refactoring easier.
Tip: If you change a type name with refactoring, a var type local will not need to be changed.