implicit type var

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?


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 xY 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 xY .

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.


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.


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.

链接地址: http://www.djcxy.com/p/53822.html

上一篇: 我的代码有什么区别?

下一篇: 隐式类型var