0

Possible Duplicate:
Use of var keyword in C#

Being relatively new to C# I was wondering the motivation MS had to introduce the var implicit typed variables. The documentation says:

An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

Some lines further:

In many cases the use of var is optional and is just a syntactic convenience

This is all nice but in my mind, this will only cause confusion.

Say you are reviewing this for loop.

foreach (var item in custQuery){
    // A bench of code...
}

Instead of reviewing the content and the semantics of the loop, I would loose precious time looking for the item's type! I would prefer the following instead:

foreach (String item in custQuery){
    // A bench of code...
}

The question is: I read that implicit typed variables help when dealing with LINQ, does really help to use it in other scenarios?

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
  • 4
    If you're working in Visual Studio, you can pause your cursor over the `var` keyword and it'll tell you the actual type. – BoltClock Nov 26 '11 at 11:38
  • Maybe this can help you: http://stackoverflow.com/questions/650919/using-implicitly-typed-local-variables – NotGaeL Nov 26 '11 at 11:41

3 Answers3

5

The var keyword was needed when LINQ was introduced, so that the language could create a strongly typed variable for an anonymous type.

Example:

var x = new { Y = 42 };

Now x is a strongly typed variable that has a specific type, but there is no name for that type. The compiler knows what x.Y means, so you don't have to use reflection to get to the data in the object, as you would if you did:

object x = new { Y = 42 };

Now x is of the type object, so you can't use x.Y.

When used with LINQ it can for example look like this:

var x = from item in source select new { X = item.X, Y = item.Y };

The x variable is now an IEnumerable<T> where T is a specific type which doesn't have a name.

Since the var keyword was introduced, it has also been used to make code more readable, and misused to save keystrokes.

An example where it makes the code more readable would be:

var list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

instead of:

System.Collections.Generic.List<System.Windows.Forms.Message.Msg> list =
  new System.Collections.Generic.List<System.Windows.Forms.Message.Msg>();

This is a good use of the var keyword, as the type already exists in the statement. A case where the keyword can be misused is in a statement like:

var result = SomeMethod();

As the SomeMethod name doesn't give any indication of what type it returns, it's not obvious what the type of the variable will be. In this case you should write out the type rather than using the var keyword.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

I think some of the motivation was to allow something like this -

List<int> list = new List<int>();

to be turned into this -

var list = new List<int>();

The second example is shorter and more readable, but still clearly expresses the intent of the code. There are instances when it will be less clear, but in lots of situations you get conciseness with no loss of clarity.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • 1
    And if for -any- reason the right hand side has to have a fully-qualified name, this becomes even more effective. – BoltClock Nov 26 '11 at 11:42
  • 1
    i don't buy easier to type as a reason to use it. keyboardig is (ok, should be) a core competency for developing software and saving a few keystrokes (especially with intellisense) is not that compelling. easier to read is the argument that should be put forward here. getting rid of the repetition is a boost for readability, which is significant. typability is not an important point. – Dave Rael Nov 26 '11 at 12:21
  • +1 I agree that it's not easier to type so much thanks to VS and how awesome input is, but it does cut down on possible typos which is good. But ultimately it makes it so much more readable! – Gaijinhunter Nov 26 '11 at 12:29
1

var is really needed for anonymous types, which are used in Linq a bit:

var results =
    from item in context.Table
    select new {Name=item.Name, id=item.id};

Since the collection is of an anonymous type, it can not be named. It has a real type, but not one with a name before compilation.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123