C# 初始化变量

发布于 2024-12-02 20:30:18 字数 395 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞旧 2024-12-09 20:30:18

显式变量类型的使用与 var 只是一个偏好问题。您可以在 resharper 中将其关闭。

在此处输入图像描述

The explicit variable type usage vs var is just a matter of preference. You can turn it off in resharper.

enter image description here

遗弃M 2024-12-09 20:30:18

如果这就是懒惰,那么我就不想高效。

Dictionary<int, Dictionary<string, string>> map = 
    new Dictionary<int, Dictionary<string, string>>();

var map = new Dictionary<int, Dictionary<string, string>>();

If this is lazy then I don't want to be productive.

Dictionary<int, Dictionary<string, string>> map = 
    new Dictionary<int, Dictionary<string, string>>();

var map = new Dictionary<int, Dictionary<string, string>>();
提笔落墨 2024-12-09 20:30:18

使用 var 只是使用隐式类型,而不是显式调用类型,使用 var 声明的变量仍然是强类型的,并且会被编译下来与显式类型变量相同的代码 - 没有开销。

唯一存在差异的情况是当您希望变量的类型与分配给它的表达式不同时,即当表达式返回实现该接口的具体类时,您希望变量具有接口的类型。在这些情况下,您应该显式声明变量。

using var is just using implicit typing instead of explicitly calling out the type, a variable declared with var 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.

梦在深巷 2024-12-09 20:30:18

var 没有运行时开销,并且编译时开销相当小。它生成的 IL 代码实际上相当于使用显式类型。您是否喜欢使用它完全取决于您的偏好。我个人将 Resharper 设置为不会打扰我,除非初始化使类型变得明显:

Person p = new Person(); // Resharper bugs me
int i = 0; // Resharper leaves this alone.

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:

Person p = new Person(); // Resharper bugs me
int i = 0; // Resharper leaves this alone.
天赋异禀 2024-12-09 20:30:18

关键是,当您直接使用 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

我恋#小黄人 2024-12-09 20:30:18

var 本身并不是一种类型。它只是命令编译器在初始化的右侧查找类型,这就是为什么以下内容无法编译的原因:

var x; // invalid because x is still statically typed 

因此,它既不会对编译时间产生不良影响,也不会产生任何运行时开销。它基本上是语法糖,生成的“机器”代码将是相同的。

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:

var x; // invalid because x is still statically typed 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文