According to MSDN, the definition of var is...
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.
I find this interesting. When I was first exposed to the var keyword, I mistakenly assumed it was for use with just LINQ queries like the code below:
private void ExampleMethod()
{ var file = from d in System.IO.Directory.GetFiles("c:\\") select d;
}
Var is of type IEnumerable in the case above. As it turns out, this is not the only use for var. It can be used for any locally scoped variable.
This is something as a former VB6 programmer I shudder to think about. VB6 had a variant data type. This was not only a bad idea, but it was horribly misused by inexperienced programmers. Just to cover all the bases, it was bad for reading the code, and it was bad for performance.
The var data type in C# is different, it is not like a variant at all. It is assigned the variable type from the result of an assignment. Lets run a test:
public static void TestMethod1()
{ var isAString = "ABC";
Console.WriteLine(isAString.GetType().ToString());
}
I would like to point something out. Intelli-sense treated the isAString variable as a string as I was typing the Console.WriteLine function call arguments. I am expecting to see String output to the Console. It said System.String but close enough ;). This tells us the variable is strongly typed. The compiler inferred its type from the assignment. This makes for some good variable declaration short cuts. I am not sure if that is a great idea,but If you couple that with a good variable naming conventions you might be ok.
Now let's try another test:
public static void TestMethod1()
{ var isAString = 1;
isAString = 2.02;
Console.WriteLine(isAString.GetType().ToString());
}
That code would not even compile. In case you don't see it right away, I have created a var of type int and tried to assign a double value to it. Let's see if this will work...
public static void TestMethod1()
{ var isAString = 5/7;
Console.WriteLine(isAString.GetType().ToString());
}
I am not so sure I like this, no visible compile errors this time. Lets see if it builds... It does build. Now lets see what the type is when we run it. Ok so it creates an integer and it gets the value of 0.
It would be good for temp or throw away variables. Let's try assigning it to the return type of a function. I don't think this would be a great idea.
public static void TestMethod1()
{ var isAString = getIntForTest;
Console.WriteLine(isAString.ToString());
Console.WriteLine(isAString.GetType().ToString());
}
public static int getIntForTest()
{ return 5;
}
Sweet that code would not compile. I really think allowing var to be assigned the result of a function would have been a bad idea, because it would make it more difficult to determine what the return type of the function was supposed to be. I think they did a good job with this keyword. As a developer with a little bit of experience everything I would have thought would be a bad idea does not work. I hope the VB6 guys are listening.
Alright enough blogging for tonight.
Code Happy,
Bill