C# 初始化变量
我正在 VS2010 中进行 c# 编程,并且一直在测试一个名为 ReSharper 的插件。根据我的经验,我觉得它清理了代码并简化了不必要的逻辑。然而,它在我的代码中出现的主要问题之一是当我初始化变量时。
Resparper 建议初始化机智“var”
int i = 0 ----> var i = 0
or...
MyClass1 MyClass = new MyClass(1)
我一直认为使用 var 有一点开销,并且看起来很懒惰或松散类型(尽管我确实意识到它有一些优点)。我一直在试图寻找是否有充分的理由来初始化这样的变量。 Reshaper 表示,使用 var 是一个有争议的问题,但它有很好的增强功能可用于初始化。有谁知道这可能是什么或可能不是?
I am programming c# in VS2010 and have been testing out a plugin called ReSharper. In my experience I feel it cleans up the code and simplifies unnecessary logic. However one of the main problems it picks up all over my code is when I am initializing variables.
Resparper recommends initializing wit "var"
int i = 0 ----> var i = 0
or...
MyClass1 MyClass = new MyClass(1)
I always thought that using var had a slight overhead and seems kind out lazy or loosely types (although I do realize it has some advantages). I have been having trouble trying to find if there is a good reason to initialize variables like this. Reshaper says that using var is a controversial issue but it has good augments to use for initialization. Does anyone know what this may or may not be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
显式变量类型的使用与 var 只是一个偏好问题。您可以在 resharper 中将其关闭。
The explicit variable type usage vs var is just a matter of preference. You can turn it off in resharper.
如果这就是懒惰,那么我就不想高效。
If this is lazy then I don't want to be productive.
使用
var
只是使用隐式类型,而不是显式调用类型,使用var
声明的变量仍然是强类型的,并且会被编译下来与显式类型变量相同的代码 - 没有开销。唯一存在差异的情况是当您希望变量的类型与分配给它的表达式不同时,即当表达式返回实现该接口的具体类时,您希望变量具有接口的类型。在这些情况下,您应该显式声明变量。
using
var
is just using implicit typing instead of explicitly calling out the type, a variable declared withvar
is still strongly typed and will be compiled down to the same code as the explicitly typed variable - no overhead.The only times there is a difference is when you want to have the variably typed differently than the expression it is assigned to, i.e. you want the variable to have the type of an interface when the expression returns a concrete class that implements that interface. In these cases you should declare the variable explicitly.
var
没有运行时开销,并且编译时开销相当小。它生成的 IL 代码实际上相当于使用显式类型。您是否喜欢使用它完全取决于您的偏好。我个人将 Resharper 设置为不会打扰我,除非初始化使类型变得明显:var
has no runtime overhead and fairly minimal compile-time overhead. The IL code it produces is actually equivalent to if you use an explicit type. Whether you prefer to use it or not is entirely a matter of preference. I've personally set Resharper to not bother me unless the initialization makes the type obvious:关键是,当您直接使用 new Classname(...) 分配变量时,您显然绝对且唯一地知道该变量的最终类型,因为只是调用了构造函数。
在这种情况下,var 具有很好的语法并使事情变得紧凑,其优点是如果将来您将使用另一种类型的对象更改分配,则更改量很小。
在不完全清楚分配的对象类型的情况下,我仍然喜欢将其明确化。
但最终几乎是一个哲学问题:D
the point is that when you assign a variable with a direct usage of
new Classname(...)
you clearly absolutely and uniquely know the final type of the variable, because just called the constructor.In this case var has a nice syntax and makes things compact, with the advantage that if in the future you will change the assignment with another type of object, changes are minimal.
in cases where it's not absolutely clear the type of object assigned, I still like to make it explicit.
but in the end is almost a philosophical issue :D
var
本身并不是一种类型。它只是命令编译器在初始化的右侧查找类型,这就是为什么以下内容无法编译的原因:因此,它既不会对编译时间产生不良影响,也不会产生任何运行时开销。它基本上是语法糖,生成的“机器”代码将是相同的。
var
is not a type in itself. It just orders the compiler to look up the type on the right side of initialization, which is why the following won't compile:So, neither will it have a bad effect on compilation time, nor does it have any runtime overhead. It is basically syntactic sugar, the generated "machine" code will be the same.