[英]Under what assumption does Re-Sharper shows me “Use implicitly typed local variable” message?
In my understanding C# started as a static language and with some enhancements in the .NET framework and started supporting the dynamic nature of the language. 以我的理解,C#开始是一种静态语言,并且对.NET框架进行了一些增强,并开始支持该语言的动态性质。
I think the "var" keyword in c# is very powerful when it comes to loading the DLLs at the runtime and we do not know the types coming our way, it is very helpful. 我认为,在运行时加载DLL时,c#中的“ var”关键字非常强大,而且我们不知道即将到来的类型,它非常有帮助。
But I think it brings the overhead of determining the type of the variable if the variable has been declared as a var at design-time. 但是我认为,如果在设计时已将变量声明为var,则会带来确定变量类型的开销。 Now, re-sharper which is responsible for making our code much more nicer and cleaner by doing some nice suggestions has suggested me something like below:
现在,re-sharper负责通过提出一些不错的建议使我们的代码更加漂亮和整洁,这向我建议了以下内容:
My code looks like this: 我的代码如下所示:
StatusResult result = new StatusResult();
Resharper suggests that it has to be converted into Resharper建议将其转换为
var result = new StatusResult();
Why is that? 这是为什么? why should I buy resharper's suggestion when I think that it is not a good idea?
当我认为这不是一个好主意时,为什么应该购买resharper的建议? Or may be I am wrong?
还是我错了?
This is mostly an opinion-based question, but there's one issue worth addressing: it is very important to distinguish type inference from dynamic typing. 这主要是一个基于意见的问题,但是有一个值得解决的问题:将类型推断与动态类型区别开来非常重要。
In dynamic i = 0;
dynamic i = 0;
, i
is declared as a dynamic variable, whose type is only resolved in run time. ,
i
被声明为动态变量,其类型仅在运行时解析。 It may result in an overhead. 这可能会导致开销。
In var i = 0;
在
var i = 0;
i
is declared as an int . i
被宣布为int 。 The var
keyword means only that the variable type will be inferred by the compiler at compile time. var
关键字仅意味着在编译时由编译器推断变量类型。 There's no "design-time" type inference overhead, since var i = 1;
因为
var i = 1;
,所以没有“设计时”类型推断开销var i = 1;
and int i = 1
compiles to the same set of instructions. 而
int i = 1
编译为同一组指令。 It's got nothing to do with dynamic typing. 它与动态类型无关。
As to your question, 关于你的问题
Why is that?
这是为什么? why should I buy resharper's suggestion when I think that it is not a good idea?
当我认为这不是一个好主意时,为什么应该购买resharper的建议?
Here is my opinion from my experience. 根据我的经验,这是我的看法 。 Some pro's of accepting this particular suggestion:
一些专业人士接受这个建议:
Now, if the line of code was: 现在,如果代码行是:
var result = GetStatusResult(); // A method call
I personally would not use var because a maintainer now needs to grok GetStatusResult() to see what var is. 我个人不会使用var,因为维护人员现在需要查看GetStatusResult()以查看var是什么。
At the end of the day it is a personal decision, unless you have to follow a coding standard, in which case you should lobby to change the standard or just go along with it. 归根结底,这是个人决定,除非您必须遵循编码标准,在这种情况下,您应该游说更改标准或只是遵循该标准。
As a side note, and as Benesh mentions below, var != dynamic. 作为旁注,正如Benesh在下面提到的,var!= dynamic。 I think this is where var get's a bad rap.
我认为这是var get不好的地方。 Plus the overuse in the example of when not to use it I provided above.
加上上面提供的何时不使用示例的过度使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.