Learn about var. We may have encountered the var keyword. We may have a strong C or C++ background, but frankly the var keyword looks alien to us. We want to discover its utility, if it has one. We want to know why it exists and where to use it.
The var keyword and variable type is perplexing at first when encountered in C#. We know that C# is strongly-typed, which means that memory is labeled by what kind of object it contains. I won't pretend to be your textbook, but let's look at some important properties of C#. What are some important parts of C#'s design?
C# is strongly typed, but our smart compiler (which is trying to help) can make some solid observations about our code. We can replace long declarations by short ones with var. The next code example has some var syntax demonstrations.
/// <summary>
/// Contains our var example method.
/// </summary>
class VarExample
{
/// <summary>
/// Demonstrate some usages of var keyword.
/// </summary>
public VarExample()
{
// 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.
// Display odd numbers.
// Returns IEnumerable<T> (where T is int).
var items = from item in cat
where (item % 2 == 1)
select item;
// Each item is actually an int.
foreach (var item in items)
{
Console.WriteLine(item);
}
// Above loop is exactly the same as this:
foreach (int item in items)
{
Console.WriteLine(item);
}
}
}
I have to disabuse you of some things that var can't do. You can't assign a new var to null, as this will result in the CS0825 warning. You can't use var as a parameter type or a return value of a method. Don't get clever with var.
The next example here really looks at what var does. It is just a syntax trick by the compiler you can use. First here I show you a simple method that has two declarations, each of which stores the value 5. They look quite different, but they are not.
/// <summary>
/// Make two instances of variables, and then add them together.
/// </summary>
/// <returns>Both ints added together.</returns>
public int ReturnValue()
{
var a = 5;
int b = 5;
return a + b;
}
Intermediate Language (IL) is what C# code is turned into (before it is JIT compiled). What is next is some of that output, which I examined using a utility called IL DASM. (The utility comes packaged with Visual Studio Professional.) See how there are two int32 values in the IL below. Var is exactly the same thing as int to the IL.
.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
Var is purely a syntax extension, and you can find out what it stands for by hovering over it in Visual Studio. Really it changes nothing about C#. It could be thought of like a #define or typedef in C++. My recommendation is to only use it around code that uses LINQ, and not in most code where the declaration can provide more visual clues.