
You are unfamiliar with the var keyword in the C# programming language and want to look at an example of it with explanations. It doesn't seem to fit with the rest of the language. This short information sheet has an example of var along with some discussion, with code in the C# language.
First, we look at some examples of using this new keyword in the C# language. Here is a code example of the var keyword being used in three different contexts in C# 3.0. You need the .NET Framework 3.5 to run this.
=== Program that uses var keyword (C#) ===
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main()
{
// 1.
// var is always available, not just in Linq.
var cat = new List<int>
{
1,
2,
7,
9
};
// Hover over the 'var' keyword and Visual Studio 2008
// will tell you what it really is.
// 2.
// This example display odd numbers.
// Returns IEnumerable<T> (where T is int).
var items = from item in cat
where (item % 2 == 1)
select item;
// 3.
// Each item is an int.
foreach (var item in items)
{
Console.WriteLine(item);
}
// The previous loop is the same as this:
foreach (int item in items)
{
Console.WriteLine(item);
}
}
}
=== Output of the program ===
1
7
9
1
7
9Description of the program. It uses var and List. The variable cat is known to be a List. Hover over the "var" keyword and the compiler will tell you that it's a List. Here we see the var items query. It returns "IEnumerable<int>". You can use the foreach loop with var. You can use the var item exactly like you can use any type.
The C# language is strongly-typed, which means that memory is labeled by what kind of object it contains. The language uses strong types to enforce code quality. Variables are both memory and also type annotations. It raises errors and warnings because it wants your code to work properly. It makes inferences about your code and can deduce the type of a variable without you telling it exactly.
Brevity of the language. The C# programming language is wordy. We don't like entering text at all. Languages like C# and Java sometimes have very long keywords. You can reduce this with var, which allows a three-letter type to be used.
Var limitations. Here we look at an interesting limitation of the var keyword. You can't assign a new var to null. This will result in the CS0825 warning. You can't use var as a parameter type or a return value of a method.
Here we look at the internal implementation and behavior of the var keyword in the C# language. IL is what C# code is turned into. You can see it with a utility called IL DASM or Reflector by Red Gate. There are two int32 values in the IL below.
=== Method using var (C#) ===
public int ReturnValue()
{
var a = 5;
int b = 5;
return a + b;
}
=== IL of the method (.NET 3.5 SP1) ===
.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::ReturnValueDescription of the intermediate language. The int declaration is exactly the same as the var declaration in the IL. So the .NET runtime doesn't know that you used one or the other. Because they are compiled to the same IL, the var keyword is therefore equally fast as strong types.
Here we saw how you can use the var keyword in the C# programming language. Find out what it stands for by hovering over it in Visual Studio. It has similarities to #define or typedef in C++. Use var for brevity and where it makes the code easier to understand. The var keyword has equal performance and does not affect runtime behavior in any way.