What is the significance of "var" keyword in c#.Net?
Possible Duplicate:
Use of var keyword in C#
What's the point of the var keyword?
I am a bit confused here in using Implicit typing over Explicit typing.
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?
What would be the advantage?
Kindly help to clarify my doubt.
take this as an example:
var q = from c in myCollection
select new
{ c.Value1, c.Value2 };
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
when we already know the underlying variable type, why do we need to use implicit typing here? What would be the advantage?
It's just for coding convinience, fast typing, if you wish, plus your line becomes shorter.
At the end, the type is visible on the right side of assignment, in that case.
When you know the underlying type, the only advantage you get from implicitly typed variables is not having to redefine them if you ever change the original type (for a compatible one).
Basically, the best advantage in implicitly typed variable is assigning types which tend to be long and ever-changing (like linq enumerables or queryables)
链接地址: http://www.djcxy.com/p/53814.html上一篇: 何时使用var和什么时候使用强类型?