C# 为什么 ReSharper 想要对所有内容都使用“var”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1873873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does ReSharper want to use 'var' for everything?
提问by Chris
I've just started using ReSharper with Visual Studio (after the many recommendations on SO). To try it out I opened up a recent ASP.NET MVC project. One of the first and most frequent things I've noticed it suggesting is to change most/all my explicit declarations to var
instead. For example:
我刚刚开始在 Visual Studio 中使用 ReSharper(在 SO 上的许多建议之后)。为了尝试一下,我打开了一个最近的 ASP.NET MVC 项目。我注意到它建议的第一个也是最常见的事情之一是将我的大部分/所有显式声明var
改为。例如:
//From This:
MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
//To This:
var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
and so on, even with simple types such as int
, bool
, etc.
等,即使采用简单的类型,如int
,bool
等
Why is this being recommended? I don't come from a computer science or .NET background, having "fallen into" .NET development recently, so I'd really like to understand what's going on and whether it's of benefit or not.
为什么推荐这个?我不是来自计算机科学或 .NET 背景,最近“陷入”了 .NET 开发,所以我真的很想了解正在发生的事情以及它是否有益。
采纳答案by Mark Sherretta
One reason is improved readability. Which is better?
原因之一是提高了可读性。哪个更好?
Dictionary<int, MyLongNamedObject> dictionary = new Dictionary<int, MyLongNamedObject>();
or
或者
var dictionary = new Dictionary<int, MyLongNamedObject>();
回答by eWolf
There is no technical difference, if you use var, the type is implied by the compiler. If you have a code like this:
没有技术上的区别,如果你使用 var,类型是由编译器隐含的。如果你有这样的代码:
var x = 1;
x is implied to be an int and no other values can be assigned to it.
x 隐含为 int 并且不能为其分配其他值。
The var keyword is useful if you change the type of the variable; you then only have to make one change instead of two:
如果您更改变量的类型,则 var 关键字很有用;然后,您只需进行一项更改而不是两项更改:
var x = 1; --> var x = "hello";
int x = 1; --> string x = "hello";
回答by Bryan Menard
I personally prefer to turn this suggestion off. Using var
can often improve readability; but as you mentioned, it sometimes reduces it (with simple types, or when the resulting type is obscure).
我个人更喜欢关闭这个建议。使用var
往往可以提高可读性;但正如您所提到的,它有时会减少它(使用简单类型,或者当结果类型模糊时)。
I prefer to choose when I use var
and when I don't. But again, that's just me.
我更喜欢选择何时使用var
和何时不使用。但同样,那只是我。
回答by LiamB
I disliked this as well.
我也不喜欢这个。
I dont want this to turn into a debate on the use of var
, it has its uses but should not be used everywhere.
我不希望这变成关于使用的争论var
,它有它的用途,但不应该在任何地方使用。
The key thing to remember is ReSharper is configured to whatever coding standards you want.
要记住的关键是 ReSharper 配置为您想要的任何编码标准。
Edit: ReSharper and var
回答by Paul Sasik
ReSharper recommends var because it tends to unclutter object creation.
ReSharper 建议使用 var,因为它倾向于使对象创建变得整洁。
Compare these two examples:
比较这两个例子:
StringBuilder bld = new StringBuilder();
var bld = new StringBuilder();
It's just a shorthand that is supposed to be easier to read.
这只是一个应该更容易阅读的速记。
i think it's fine when you create new objects explicitly with "new". In your example however, it might not be obvious if the classes were not named properly.
我认为当您使用“new”显式创建新对象时很好。但是,在您的示例中,如果类命名不正确,则可能并不明显。
回答by Klaus Byskov Pedersen
The var
feature of .Net 3.0 is just type inference, which is type safe and often makes your code easier to read. But you don't have to, and can turn that recommendation off in resharper if you want.
var
.Net 3.0的特性只是类型推断,它是类型安全的,通常使您的代码更易于阅读。但是您不必这样做,并且可以根据需要在 resharper 中关闭该建议。
回答by Guffa
What ReSharper suggests is clearly overuse of the var keyword. You can use it where the type is obvious:
ReSharper 的建议显然是过度使用 var 关键字。您可以在类型明显的地方使用它:
var obj = new SomeObject();
If the type is not obvious, you should rather write it out:
如果类型不明显,你应该把它写出来:
SomeObject obj = DB.SomeClass.GetObject(42);
回答by Tim Robinson
BTW, ReSharper draws a distinction between 'you might want to apply this suggestion to your code' and 'your code is broken, want me to fix it?'. The var
keyword is in the suggestion category, along with things like "invert if to reduce nesting"; you don't have to follow it.
顺便说一句,ReSharper 区分了“您可能想将此建议应用于您的代码”和“您的代码已损坏,要我修复它吗?”。该var
关键字是建议类,与喜欢的东西“吊装如果减少筑巢”一起; 你不必遵循它。
You can configure how annoying each of its alerts are through the Options dialog, or directly though the popup menu for that alert. You can downgrade things like the var
suggestion so they're less prominent, or you can upgrade things like the 'use extension method' alert so it shows up as an actual error.
您可以通过“选项”对话框或直接通过该警报的弹出菜单配置每个警报的烦人程度。您可以降级var
建议之类的内容,使其不那么突出,或者您可以升级诸如“使用扩展方法”警报之类的内容,使其显示为实际错误。
回答by Daniel May
The var
keyword was introduced in C# 3.0 - it allows us to forget about specifying our type explicitly.
该var
关键字是在 C# 3.0 中引入的 - 它允许我们忘记显式指定我们的类型。
There is no real difference to whether you use
你是否使用并没有真正的区别
MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
or
或者
var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
except pure readability and less chance for error.
除了纯粹的可读性和更少的出错机会。
It seems like a clichéd example, but say the following may help your understanding:
这似乎是一个陈词滥调的例子,但说以下可能有助于您的理解:
var myInt = 23;
returns an int
type, whereas
返回一个int
类型,而
var myInt = "23";
returns a string
type.
返回一个string
类型。
回答by Jaco Pretorius
There is no technical difference (as eWolf pointed out). You can use one or the other, the generated CLR code will look the same.
没有技术差异(正如 eWolf 指出的那样)。您可以使用其中一种,生成的 CLR 代码看起来是一样的。
In my opinion the main benefit is that this tends to force you to use better variable naming. In your example 'foo' is a pretty poor choice for a variable name.
在我看来,主要的好处是这往往会迫使您使用更好的变量命名。在您的示例中, 'foo' 是一个非常糟糕的变量名称选择。