Possible Duplicate:
ReSharper and var可能重复:ReSharper和var
After I have installed ReSharper it demands(by warnings) that I use var whenever possible, for example
在安装ReSharper之后,我需要(通过警告)尽可能地使用var
UnhandledExceptionEventArgs ue = (UnhandledExceptionEventArgs) t;
ReSharper wants to turn it into
ReSharper想要把它变成
var ue = (UnhandledExceptionEventArgs) t;
I like the first version better, is there any reason to prefer var? better performance? anything? or is it just a code style?
我更喜欢第一个版本,有什么理由喜欢var吗?更好的性能吗?什么吗?还是仅仅是一种代码风格?
4 个解决方案
#1
87
It's really just a coding style. The compiler generates the exact same for both variants.
它只是一种编码风格。编译器为两个变体生成完全相同的结果。
See also here for the performance question:
关于性能问题,请参见这里:
- Will using 'var' affect performance?
- 使用“var”会影响性能吗?
#2
30
When you say "by warnings" what exactly do you mean? I've usually seen it giving a hint that you may want to use var, but nothing as harsh as a warning.
当你说“警告”的时候,你到底是什么意思?我通常看到它给出了一个提示,提示您可能想使用var,但是没有什么比警告更严厉的了。
There's no performance difference with var - the code is compiled to the same IL. The potential benefit is in readability - if you've already made the type of the variable crystal clear on the RHS of the assignment (e.g. via a cast or a constructor call), where's the benefit of also having it on the LHS? It's a personal preference though.
没有性能差异与var - IL代码编译相同。可读性的潜在好处是——如果你已经明确的类型变量水晶RHS的任务(例如,通过一个演员或构造函数调用),也有它的好处在哪里韩吗?这是我个人的偏好。
If you don't want R# suggesting the use of var, just change the options. One thing about ReSharper: it's very configurable :)
如果您不希望r#建议使用var,只需更改选项。关于ReSharper一点:它是可配置的
#3
11
In this case it is just coding style.
在这种情况下,它只是编码风格。
Use of var
is only necessary when dealing with anonymous types.
In other situations it's a matter of taste.
只有在处理匿名类型时才需要使用var。在其他情况下,这是一个品味问题。
#4
11
As the others have said, there is no difference in the compiled code (IL) when you use either of the following:
正如其他人所说的,当您使用下列任何一种代码时,编译代码(IL)没有区别:
var x1 = new object();
object x2 = new object;
I suppose Resharper warns you because it is [in my opinion] easier to read the first example than the second. Besides, what's the need to repeat the name of the type twice?
我想Resharper会警告你,因为在我看来,读第一个例子比读第二个例子更容易。此外,有什么必要重复两次类型的名称?
Consider the following and you'll get what I mean:
考虑以下内容,你就会明白我的意思:
KeyValuePair<string, KeyValuePair<string, int>> y1 = new KeyValuePair<string, KeyValuePair<string, int>>("key", new KeyValuePair<string, int>("subkey", 5));
It's way easier to read this instead:
读这篇文章要容易得多:
var y2 = new KeyValuePair<string, KeyValuePair<string, int>>("key", new KeyValuePair<string, int>("subkey", 5));
#1
87
It's really just a coding style. The compiler generates the exact same for both variants.
它只是一种编码风格。编译器为两个变体生成完全相同的结果。
See also here for the performance question:
关于性能问题,请参见这里:
- Will using 'var' affect performance?
- 使用“var”会影响性能吗?
#2
30
When you say "by warnings" what exactly do you mean? I've usually seen it giving a hint that you may want to use var, but nothing as harsh as a warning.
当你说“警告”的时候,你到底是什么意思?我通常看到它给出了一个提示,提示您可能想使用var,但是没有什么比警告更严厉的了。
There's no performance difference with var - the code is compiled to the same IL. The potential benefit is in readability - if you've already made the type of the variable crystal clear on the RHS of the assignment (e.g. via a cast or a constructor call), where's the benefit of also having it on the LHS? It's a personal preference though.
没有性能差异与var - IL代码编译相同。可读性的潜在好处是——如果你已经明确的类型变量水晶RHS的任务(例如,通过一个演员或构造函数调用),也有它的好处在哪里韩吗?这是我个人的偏好。
If you don't want R# suggesting the use of var, just change the options. One thing about ReSharper: it's very configurable :)
如果您不希望r#建议使用var,只需更改选项。关于ReSharper一点:它是可配置的
#3
11
In this case it is just coding style.
在这种情况下,它只是编码风格。
Use of var
is only necessary when dealing with anonymous types.
In other situations it's a matter of taste.
只有在处理匿名类型时才需要使用var。在其他情况下,这是一个品味问题。
#4
11
As the others have said, there is no difference in the compiled code (IL) when you use either of the following:
正如其他人所说的,当您使用下列任何一种代码时,编译代码(IL)没有区别:
var x1 = new object();
object x2 = new object;
I suppose Resharper warns you because it is [in my opinion] easier to read the first example than the second. Besides, what's the need to repeat the name of the type twice?
我想Resharper会警告你,因为在我看来,读第一个例子比读第二个例子更容易。此外,有什么必要重复两次类型的名称?
Consider the following and you'll get what I mean:
考虑以下内容,你就会明白我的意思:
KeyValuePair<string, KeyValuePair<string, int>> y1 = new KeyValuePair<string, KeyValuePair<string, int>>("key", new KeyValuePair<string, int>("subkey", 5));
It's way easier to read this instead:
读这篇文章要容易得多:
var y2 = new KeyValuePair<string, KeyValuePair<string, int>>("key", new KeyValuePair<string, int>("subkey", 5));