可能顯示的文件:
Why class fields cannot be var?
Implicit typing; why just local variables?
var in C# - Why can't it be used as a member variable?爲什麼VAR不能被應用到成員變量
了var類型不能被施加到成員變量。我想知道爲什麼?
可能顯示的文件:
Why class fields cannot be var?
Implicit typing; why just local variables?
var in C# - Why can't it be used as a member variable?爲什麼VAR不能被應用到成員變量
了var類型不能被施加到成員變量。我想知道爲什麼?
Eric Lippert對此有一個detailed blog post。
摘要:
我很喜歡它的簡單情況,但「簡單」的平衡可能很難以「感覺正確」的優雅方式來決定。
嗚好的博客貼子,都沒有看到它,乾杯喬恩。 – Maciek 2011-01-21 08:04:24
var只能在本地範圍內使用。 實際的類型必須由編譯器來推斷,這是太硬/不可能從這樣的結構確定類型:
class Person
{
public var Name {get; set;}
}
簡單地說:
// code fragment
int Add(int x, int y) { return x + y; }
var x = Add(int x, int y); // the compiler will know that the result of the call will be of type int
class X
{
var x; // illegal, the compiler has no idea what's the target type of X
}
我不認爲這是問題的範圍。想'class X { var x = 5; }`現在回答這個.. – nawfal 2012-07-04 19:02:00
因爲它的類型未知。這與編譯器的構建順序有關。
你也可以看看this topic.
又重複:http://stackoverflow.com/q/2771485/310574 – Gabe 2011-01-21 08:05:55