C#中var关键字的使用

C#中var关键字的使用

问题描述:

可能显示的文件:

使用什么:?VAR或对象名称类型

var关键字使用在C#

什么&rsquo的;?s var关键字的点

我应该总是的青睐implictly类型的局部变量C#3.0?

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 always favour implictly typed local variables in C# 3.0?

我刚装了ReSharper的试用版,以评估它为我的公司。我注意到一件事是它是暗示我更改以下(由为例):

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):

string s = "";



to

var s = "";



这是声明变量时使用var关键字,而不是使用对象类型的最佳实践?没有给出什么优势。对于语境我是谁刚刚过渡到.NET前Java开发人员的工作。

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 其中,它使代码更易于阅读,这对我来说将意味着类型 VAR 正在取代必须完全。显而易见

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

例如,这将是一个良好的使用变种(人为的例子):

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

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



不过,这将是一个不好用变种

var thing = GetThingFromDatabase();