Use var operator instead of class type

Possible Duplicate:
Use of var keyword in C#

I use resharper in Visual Studio. Every time when I create class instance resharper suggest me to use var instead of exact type of class. Why using var operator more appropriate?


ReSharper is suggesting you to use var via a context action.

A context action in ReSharper (commonly designated by a pencil icon), much like an intention action in IntelliJ IDEA, is something that you can use if you like to, but it's not in any way obligatory. This is opposed to a quick-fix (a yellow or red bulb) that is specifically targeted to fix a code issue that ReSharper has discovered.

Time and again ReSharper users get stunned when they see ReSharper suggesting using var, then (as soon as you've accepted the suggestion) suggesting using explicit type specifier again. But considering the nature of context actions, it's completely fine to have pairs of opposite context actions like this, and ReSharper has plenty of them.

If you don't want ReSharper to display this particular context action, you can turn it off in ReSharper > Options > [Language] > Context actions


Less repetition, for starters. Which one do you like more:

IDictionary<string, object> foo = new SortedDictionary<string, object>();

or

var foo = new SortedDictionary<string, object>();

Personally, I find the latter much more readable.

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

上一篇: 隐式类型var

下一篇: 使用var运算符而不是类类型