使用隐式类型局部变量
我刚刚安装了 ReSharper 的试用版,我注意到的第一件事就是它总是建议用隐式类型的局部变量替换显式类型的局部变量,例如:
public string SomeMethod(int aParam)
{
int aNumber = SomeOtherMethod(aParam);
// should be changed to:
var aNumber = SomeOtherMethod(aParam);
}
我认为显式类型的变量更具可读性(更明确)。
您对 ReSharper 的建议有何看法? 使用隐式类型变量有什么优点吗? 什么时候使用隐式/显式变量?
I just installed a trial version of ReSharper and one of the first things I noticed is that it always suggests to replace explicitly typed local variables with implicitly typed ones, e.g:
public string SomeMethod(int aParam)
{
int aNumber = SomeOtherMethod(aParam);
// should be changed to:
var aNumber = SomeOtherMethod(aParam);
}
I think explicitly typed variables are more readable (more explicit).
What do you think about ReSharper's suggestion? Is there any advantage in using implicitly typed variables? When do you use implicit/explict vars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人只在通过阅读声明就可以清楚地区分变量类型时才使用“var”,例如:
在上面的示例中,很明显“var”指的是“List”。
当我必须去某些方法定义来找出“var”代表什么变量类型或者必须依赖于 Visual Studio Intelli-popup 或任何被调用的东西时,我不喜欢使用“var”,例如我觉得不太好:
我的意思是,“SomeMethod”函数应该返回什么? 只看这行代码你就能看出来吗? 不,你不能,所以这就是为什么我在这些情况下避免使用“var”。
I personally only use “var” when I can clearly distinguish the variable Type by just reading the declaration, for example:
In the example above, its evident that “var” refers to “List<int>”.
I don’t like to use “var” when I have to go to some method definition to find out what variable type “var” represents or by having to rely on visual studio intelli-popup or whatever that is called, for example this in not ok to me:
I mean, what is the “SomeMethod” function supposed to return? Can you tell just by looking at the line of code? No you can’t, so that is why I avoid using “var” on those situations.
关于这一点有很多讨论,但我认为这一切都取决于个人品味,就像几乎在任何地方都使用“this”关键字一样。
我个人更喜欢显式类型变量,但是当使用嵌套泛型集合时,使用隐式类型变量可以变得更具可读性。 看看:
vs:
编辑:这个SO主题涵盖了相同的主题,并有一些不错的回复: 使用什么:var 或对象名称类型?
编辑2:现在大量使用异步工作,我发现使用显式类型化变量有时可以防止讨厌的错误。 考虑这个愚蠢的示例,您希望返回用户的 ID。 另请考虑
GetUserAsync
返回Task
。 如果您使用隐式类型变量,您最终会使用类似这样的内容:这可以编译,但它是错误的。 “user”实际上是一个
Task
。 它编译为Task
也有一个Id
属性。 在这种情况下,人们可能会意外返回任务的 ID,而不是用户。上面的代码无法编译,因为编译器会抱怨您无法将任务转换为用户。 添加
await
关键字当然可以解决这个问题。我实际上曾经遇到过这种情况:-)
There's a lot of discussion about this, but I think it all comes down to personal taste, just like using the 'this' keyword almost everywhere.
I personally prefer explictly typed variables, but when using nested generic collections things can become more readable using an implicitly typed variable. Look at:
vs:
EDIT: this SO topic covers the same topic, with some nice replies: What to use: var or object name type?
EDIT2: Working a lot with async nowadays, I find that using explicity typed variables can sometimes prevent nasty bugs. Consider this silly example where you would want to return the Id of a user. Also consider that
GetUserAsync
returns aTask<User>
. If you use implicitly typed variables, you would end up using something like this:This compiles, but it is wrong. 'user' is actually a
Task<User>
. And it compiles asTask
also has anId
property. In this case, one would accidentally return the Id of a Task instead of the User.The above does not compile, as the compiler will complain that you cannot cast a Task to a User. Adding the
await
keyword of course solves this.I've actually had this happen to me once :-)
以防万一有些人还没有注意到,您可以轻松更改 Reshaper 中的“建议”(Reshaper -> 选项 -> 语言 -> 上下文操作 -> “用 'var' 替换显式类型规范”)。
我个人更喜欢到处都有明确的类型规范,但我对此并不太挑剔。
Just in case some haven’t noticed yet, you can easily change the “suggestions” in Reshaper (Reshaper -> Options -> Languages -> Context Actions -> “Replace explicit type specification with ‘var’”).
I personally prefer to have explicit type specifications everywhere, but I'm not too fussy about it.
有时键入 var 伪关键字比键入巨大的类型名称更容易,尤其是在可能涉及泛型的情况下。 但是,您应该知道它们在功能上是相同的。 无论哪种方式,都没有性能差异或任何差异。 编译器派生赋值右侧的类型,并将 var 替换为该类型。 它不像 VB 变体那样在运行时发生。
It's just easier to type the var pseudo-keyword at times than a huge type name, especially if a generic could be involved. However, you should know they're functionally identical. There's no performance difference or anything either way. The compiler derives the type of the right-side of the assignment and replaces var with that type. It's not happening at run-time like a VB variant.
FWIW,var 关键字在很多情况下都是清晰可读的。 特别是如果...
赋值的右侧是构造函数表达式。
var map = new Dictionary>();
局部变量有好名字。
华泰
FWIW, the var keyword is plainly readable in many cases. Especially if...
The right-side of the assignment is a constructor expression.
var map = new Dictionary>();
Local variables have good names.
HTH