开发者

When should the var type be used in c#? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What’s the point of the var keyword?

What is the var type and should it be used in c#? When should it be used?

Can you specify the v开发者_如何学Pythonar type for the parameters to a method?


var is not a type. It is a convinient short hand notation that saves you a lot of typing. The following two declarations are equivalent:

Dictionary<string,List<int>> dic = new Dictionary<string,List<int>>();

and

var dic = new Dictionary<string,List<int>>();

As you can see, this saves typing and are easier to read. And if you descide to change the data type, you only have to change the right hand part.

But where I really like it most is in foreach loops:

foreach ( var item in someList )

Here item automatically takes the correct type, depending on the type of someList and it enables you to change the type of someList without haveing to change the declaration of item.

In short: var is GREAT and should be used.

EDIT: No, you can not specify var as a parameter to a function. The closest you can get to that is generics, I guess. And I also completely forgot to mention anonymous types. With those you have to use var.


It is part of the language, so yes, it should be used. It is not a type, however, but signifies a placeholder for the actual type determined by the compiler.

When should you use it? This has been asked before, many times:

  • Use of var keyword in C#
  • https://stackoverflow.com/questions/633474/c-do-you-use-var


var is not a type, it's an instruction to the compiler to let it figure out the correct type to use.

This means there is no difference at all to the output of the following two lines:

var s = "Test";
string s = "Test";

As for when to use it, you'll have to come up with your own rules. There is very few things you can do with var that you cannot do with a specific type name, but there is one thing: anonymous types.

You can do:

var x = new { A = 10 };

but you cannot come up with a strong type for that, since it has no type known at compile-time, instead the compiler will create the type during compilation, and then var will use that type.

That's what var was created to support, that it can also be used without anonymous types is a bonus, but you'll have to make up your own rules for when to use it in those cases.

Personally I only use it when the type is easily read from the source code, so I would use it for:

var dict = new Dictionary<int, string>();
var stream = new FileStream(...);

but not for:

var dict = GetDataStructure();
var stream = OpenFileForReading();


We tend to use it when interacting with all of our home grown libraries.

This is because we often change things as we are developing the libraries and quite often changing the type name or return type won't actually break the things which are using it.

We also tend to use it most of the time with the .NET framework as well.

However, we have put in an outright ban on using it with native types like int, string, bool etc. because it is not always clear what type the variable is in those cases.


Well, as mentioned earlier var is a short notation for a type name of any variable. As for me, I find, that var is overused by many developers and I think var makes code less readable sometimes. But one case where u should use it definitely is anonimous type variables.


Just to add to the other commentary, I suggest using var only when you can infer the type just by reading the code

var x = new Widget(); //ok
var y = Customer.Create(); // ok 

var z = DoSomething(); //not ok


Some of the answers introduce Anonymous Types.

The var keyword on MSDN.

Know that var is not a type but a placeholder and can only be used within methods or properties (local variable definition). You can't use it in local member definition (private members of a class), neither can you when defining a method that should return whatever type, as generics, where you can use whatever name, except var (or any other reserved keyword) like so:

public IList<var> GetItems() { // Here the compiler will complain about the context of using `var` keyword within a local variable definition only.
}

public IList<T> GetItems() { // Here, we're talking about generics.
}

So, here are the principal rules to follow using the var keyword:

  1. Local variable definition only;
  2. Use var with anonymous types (Linq query, for instance);
  3. Not to be confused with generic types;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜