C# Resharper:vars
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/296783/
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
Resharper: vars
提问by CSharpAtl
Why does Resharper want you to change most variables to var type instead of the actual type in the code?
为什么 Resharper 要你把大部分变量改成 var 类型而不是代码中的实际类型?
采纳答案by Jon Skeet
It's just an option. You can disable it:
这只是一种选择。您可以禁用它:
ReSharper -> Options -> Code Inspection -> Inspection Severity -> Code Redundencies -> Use 'var' keyword where possible: change this to "Do not show"
ReSharper -> 选项 -> 代码检查 -> 检查严重性 -> 代码冗余 -> 尽可能使用“var”关键字:将其更改为“不显示”
There's also the context (lightbulb) option which will take you in each direction - this is under ReSharper -> Options -> Languages -> C# -> Context Actions -> "Replaces explicit type declaration with 'var'"
还有上下文(灯泡)选项可以带你到各个方向 - 这是在 ReSharper -> 选项 -> 语言 -> C# -> 上下文操作 ->“用‘var’替换显式类型声明”
回答by Jonas Kongslund
Vars help to make the code more readable inside a method, especially if you use generics.
变量有助于使方法内的代码更具可读性,尤其是在使用泛型时。
As Jon says, it is just an option.
正如乔恩所说,这只是一种选择。
回答by Kyle West
To answer your question ... because someone at JetBrains decided that was the "preferred" way.
回答您的问题……因为 JetBrains 的某个人认为这是“首选”方式。
To change it follow Jon's answer. Additionally you can also change ReSharper's behavior when doing Code Cleanup (which I use a lot) under the Code Cleanup section in ReSharper options. Set to "Use Explicit Type".
要更改它,请遵循 Jon 的回答。此外,您还可以在 ReSharper 选项的代码清理部分下执行代码清理(我经常使用)时更改 ReSharper 的行为。设置为“使用显式类型”。
回答by David Thibault
By default, it will "green squiggle" declarations of this type :
默认情况下,它将“绿色波浪线”这种类型的声明:
Person p = new Person();
^^^^^^
Because of the repetition.
因为重复。
It will also suggest (small green underscore) var when it can be inferred :
它还会在可以推断时建议(绿色小下划线)var:
Person p = repository.GetPerson(1);
ˉˉˉ
In this case it can be infered because of the return type of the GetPerson method.
在这种情况下,可以根据 GetPerson 方法的返回类型推断出它。
As stated by Jon Skeet, you can disable these suggestions in resharper's options.
正如 Jon Skeet 所说,您可以在 resharper 的选项中禁用这些建议。
回答by Paul Kapustin
I think it suggests you both ways. If you have a explicit type - you can change it to var. If you have var - can change it to explicit. Just to make it faster for you to change if you think it is appropriate of course.
我认为它建议你两种方式。如果您有显式类型 - 您可以将其更改为 var。如果您有 var - 可以将其更改为显式。只是为了让你更快地改变,如果你认为这当然是合适的。
It might be good to use vars, for instance, for loop variables, when iterating a collection, so on, when the type is "implicit" for you (it is always implicit for compiler of course, when Resharper suggests it) and its absence doesn't make the code less readable. Also, I like it to shorten some declarations, which may grow quite long with generics. Like, IList(IDictionary(SomeType)) myVar = List(IDictionary(SomeType)) () would not loose much if you write "var" at the left side of the assignment.
例如,在迭代集合时,当类型对您来说是“隐式”时(当然,当 Resharper 建议它时,编译器总是隐式的)并且它不存在时,使用变量可能会很好,例如,for 循环变量不会降低代码的可读性。此外,我喜欢缩短一些声明,这些声明可能会随着泛型而变得很长。就像, IList(IDictionary(SomeType)) myVar = List(IDictionary(SomeType)) () 如果你在赋值的左边写“var”就不会松太多。
(Replace parantheses with angle brackets ;)
(用尖括号替换括号;)
Of course, I would try to use vars with care, to improve readability and not vice versa.
当然,我会尽量小心地使用 vars,以提高可读性,反之亦然。
回答by Paul Kapustin
For me, it is definitely worth the price...(even if I had to pay it myself). But it can slow down your VS. It can get really slow if you have files like 5000 lines of code.
对我来说,绝对物有所值......(即使我必须自己付钱)。但它可以减慢你的 VS。如果您有 5000 行代码之类的文件,它会变得非常慢。
What I still don't get however is why I am the only one in the team using it...
然而我仍然不明白的是为什么我是团队中唯一一个使用它的人......
回答by floatingmarbles
I saw a video from Hadi Hariri, where he was presenting Resharper 6.x. His reasoning was, if you are forcing a user to use "var", you are actually forcing him to name the variable in a more meaningful way, that way all the names are readable and make more sense.
我看到了 Hadi Hariri 的视频,他在那里展示了 Resharper 6.x。他的推理是,如果你强迫用户使用“var”,你实际上是在强迫他以更有意义的方式命名变量,这样所有的名字都是可读的并且更有意义。
回答by Ilya Chernomordik
This is the explanation on the JetBrains code inspection wiki about it: http://confluence.jetbrains.net/display/ReSharper/Use+%27var%27+keyword+when+initializer+explicitly+declares+type
这是 JetBrains 代码检查 wiki 上的解释:http: //confluence.jetbrains.net/display/ReSharper/Use+%27var%27+keyword+when+initializer+explicitly+declares+type
If you see the class to the right there is no big need to see it on the left as well. Also it saves space and reduces code if the class name is quite long. Personally I don't use var for simple types like string, int and so on but do use it for something like var dictionary = new Dictionary<string, int>()
to save space.
如果你看到右边的类,那么也没有必要在左边看到它。如果类名很长,它还可以节省空间并减少代码。就我个人而言,我不会将 var 用于字符串、int 等简单类型,而是将它用于诸如var dictionary = new Dictionary<string, int>()
节省空间之类的事情。