C# var 关键字用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1205329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:51:48  来源:igfitidea点击:

C# var keyword usage

c#styles

提问by Hyman

Possible Duplicates:
What to use: var or object name type?
Use of var keyword in C#
What’s the point of the var keyword?
Should I alwaysfavour implictly typed local variables in C# 3.0?

可能的重复项:
使用什么:var 或对象名称类型?
在 C# 中使用 var 关键字
var 关键字有什么意义?
我应该总是喜欢 C# 3.0 中隐式类型的局部变量吗?

I have just installed a trial version of ReSharper to evaluate it for my company. One thing that I have noticed is it is suggesting that I change the following (made up example):

我刚刚安装了 ReSharper 的试用版,以便为我的公司对其进行评估。我注意到的一件事是建议我更改以下内容(虚构示例):

string s = "";

to

var s = "";

Is it best practice to use the var keyword rather than using the Object Type when declaring variables? What advantages does it give. For context I am a former Java developer who has just transitioned to the .Net works.

在声明变量时使用 var 关键字而不是使用对象类型是最佳实践吗?它有什么好处。对于上下文,我是一名前 Java 开发人员,刚刚过渡到 .Net 工作。

采纳答案by Jon Grant

I think it's fine to use varwhere it makes the code easier to read, which for me would mean that the type that varis replacing must be completely obvious.

我认为使用var它使代码更易于阅读的地方很好,这对我来说意味着要var替换的类型必须完全明显。

For example, this would be a good use of var(contrived example):

例如,这将是一个很好的使用var(人为的例子):

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

However this would be a bad use of var:

然而,这将是一个不好的使用var

var thing = GetThingFromDatabase();

回答by CodeSpeaker

I find it helpful in some cases where the type declaration is very long, for example:

我发现在类型声明很长的某些情况下它很有用,例如:

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

becomes

变成

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

回答by Charlie

It amounts to the same thing because using the var keyword, the variable is implicity typed and the compiler infers the type at build time. I prefer to specify the type rather than use var in most cases, so I change my resharper settings.

它相当于同一件事,因为使用 var 关键字,变量是隐式类型的,编译器在构建时推断类型。在大多数情况下,我更喜欢指定类型而不是使用 var,因此我更改了 resharper 设置。

回答by ullmark

As i remember it Resharper just says that it could be written this way. It doesn't say that you should.

我记得 Resharper 只是说它可以这样写。它没有说你应该。

Alot of suggestions is just suggestions which I turned of the first time i saw them.. That being one of them.

很多建议只是我第一次看到它们时拒绝的建议。那是其中之一。

Another was that it said was that it is redundant to write "this.someProperty", while i think the code gets easier to read by doing that.

另一个是它说编写“this.someProperty”是多余的,而我认为这样做会使代码更容易阅读。

回答by Sorantis

Resharper also has suggestion to transform if statement to true or not true. Almost the same thing, but it depends on your style of coding. So, when you find using var more comfortable - use this suggestion.

Resharper 还建议将 if 语句转换为 true 或 not true。几乎相同的事情,但这取决于您的编码风格。所以,当你发现使用 var 更舒服时 - 使用这个建议。