The var keyword does away with the need for an explicit type declaration and I have read with interest the SO discussion of when it might be appropriate.
I have also read about (but not used) Boo which seems to take things a step further by making it optional to declare a local variable. With Boo, both the type and the declaration can be implied.
Which leads me to wonder, why did the C# language designers bother to include a var keyword at all?
Update: Yes, var supports Anonymous types, but anonymous types by themselves do not necessitate the var keyword...
var anon = new { Name = "Terry", Age = 34 };
versus
anon = new { Name = "Terry", Age = 34 };
The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.
Using var means in most cases less typing than explicitly using types. Especially with longer, nested types like dictionaries and nested types, simply using var can save some keystrokes. Also the fact that when listing multiple variables, using var for all of them will result in a 'neater' looking piece of code.
Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared.
The point of var is to allow anonymous types, without it they would not be possible and that is the reason it exists.
Without the var keyword it becomes possible to accidentally create a new variable when you had actually intended to use an already existing variable. e.g.
name = "fred"; ... Name = "barney"; // whoops! we meant to reuse name
Update: There are two related questions here, actually: 1. Why do I have to declare variables at all? 2. What use is "var" in a language that makes you declare variables?
The answers to (1) are numerous, and can be found elsewhere for this question. My answer to (2) is below:
As other commenters have said, LINQ uses this for its anonymous types. However, LINQ is actually an instance of a more general problem where the type of the right-hand side of an expression is either unknown to the programmer, or is extremely verbose. Consider:
SomeGeneric<VeryLongTypename<NestedTypename>> thing = new SomeGeneric<VeryLongTypename<NestedTypename>>();
Verbose and error-prone, right? So now they let you do this:
var thing = new SomeGeneric<VeryLongTypename<NestedTypename>>();
By reducing the duplication of information, errors are eliminated. Note that there aren't just typing errors, here: it's possible for the type of the left-hand expression to be mistyped in such a way that the compiler can silently cast from left to right, but the cast actually loses some property of the rvalue. This is even more important when the types returned by the rvalue may be unknown or anonymous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With