c# - c#.Net 中 "var"关键字的意义是什么?

标签 c# var

<分区>

Possible Duplicate:
Use of var keyword in C#
What's the point of the var keyword?

我在这里对使用隐式类型而不是显式类型感到有点困惑。

MSDN 在 C# 编码标准中引用:

Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.

我的问题是,当我们已经知道底层变量类型时,为什么我们需要在这里使用隐式类型?

有什么好处?

请帮助澄清我的疑问。

最佳答案

以此为例:

var q = from c in myCollection
        select new
    { c.Value1, c.Value2 };

我们不能在这里确定 q 的数据类型,因为它是在运行时生成的“anonymus-type”
这就是它的主要目的

关于c# - c#.Net 中 "var"关键字的意义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794547/

相关文章:

c# - 当代码块结束时,var 类型会发生什么?

javascript - MathJax JS API : How to send a var with text in it and get html out of it?

c# - 无法使用 SelectListitem 获取不同的值

c# - 在 while 循环期间文件正在被另一个进程使用 (C#)

c# - Azure Blob 存储下载文件而不是在浏览器中打开

c# 使用 Youtube data api v3 获取 youtube 播放列表

javascript - 需要帮助更改变量值以更改显示的消息

javascript - 将 var 从 js 传递到 html

c# - Resharper 政策 - 如何有条件地推荐 var?

c# - 使用 Windows.Storage 命名空间时出错