什么是"的重要性; VAR"关键字在C#.NET?
可能重复:结果
的在C#中结果
的什么是var关键字的地步?
我有点困惑在这里使用了显式类型隐式类型
I am a bit confused here in using Implicit typing over Explicit typing.
在C#中引述MSDN编码标准:
As quoted by MSDN in C# Coding Standards:
使用局部变量隐式类型时变量
的类型是显而易见的分配的右边时,或当精确
类型并不重要。
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.
我的问题是,当我们已经知道的基本变量类型,为什么我们需要在这里使用隐式类型?
My question is, when we already know the underlying variable type, why do we need to use implicit typing here?
会是怎样的优势呢?
请帮助澄清我的疑问。
以此为例:
var q = from c in myCollection
select new
{ c.Value1, c.Value2 };
我们无法确定q这里的数据类型,因为它在运行时anonymus型的产生结果
,这是它的主要目的
we can't determine the datatype of q here because it's generated at run time "anonymus-type"
and that is the main purpose of it