使用隐式类型的局部变量 [英] Using implicitly typed local variables
问题描述
我刚刚安装的 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).
你觉得什么ReSharper的的建议?是否有使用隐式类型变量什么优势?当你使用隐式/露骨瓦尔?
What do you think about ReSharper's suggestion? Is there any advantage in using implicitly typed variables? When do you use implicit/explict vars?
推荐答案
我个人只用无功的时候,我可以清楚地分辨变量类型刚刚看完声明,例如:
I personally only use "var" when I can clearly distinguish the variable Type by just reading the declaration, for example:
var someVariable = new List<int>();
在上面的例子中,其明显的是,变种是指列表与所述; INT>中。
In the example above, its evident that "var" refers to "List<int>".
我不喜欢用VAR当我必须去一些方法定义,找出哪些变量类型VAR代表或不必依赖于视觉studio软件Intelli-弹出或任何被调用,例如这也不行对我说:
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:
var someVaraible = SomeMethod();
我的意思是,什么是的someMethod功能应该返回?你可以通过查看代码行告诉?不,你不能,所以这就是为什么我避免使用无功在这些情况下。
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.
这篇关于使用隐式类型的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!