2011-01-21 65 views

回答

3

Eric Lippert對此有一個detailed blog post

摘要:

  • 即使它是可行的,這需要重大的編譯器動盪
  • 確定型可能是在某些情況下是非常複雜的,並與一些奇怪的情況下結束了 - 你會被暴露對於使用匿名類型的公共變量?

我很喜歡它的簡單情況,但「簡單」的平衡可能很難以「感覺正確」的優雅方式來決定。

+0

嗚好的博客貼子,都沒有看到它,乾杯喬恩。 – Maciek 2011-01-21 08:04:24

1

var只能在本地範圍內使用。 實際的類型必須由編譯器來推斷,這是太硬/不可能從這樣的結構確定類型:

class Person 
{ 
    public var Name {get; set;} 
} 
0

簡單地說:

// 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 
} 
+1

我不認爲這是問題的範圍。想'class X { var x = 5; }`現在回答這個.. – nawfal 2012-07-04 19:02:00

0

因爲它的類型未知。這與編譯器的構建順序有關。

你也可以看看this topic.

相關問題