2011-01-28 39 views
0

我嘗試使用AIP自動實現的屬性錯誤

public int AIP_NoSet 
{ 
    get { ;} 
} 

編譯器說,這是一個錯誤:
Program.c1.AIP_NoSet.get':不是所有的代碼路徑返回一個值

但即使我寫

public int AIP_NoSet { get { ;} set { ;} }

它顯示我同樣的錯誤。

我在哪裏錯了?

回答

2

片刻derp。

public int AIP_NoSet { get; set; } 

聽起來像是你想只用 'GET' 定義的自動屬性。

這是編譯器不允許的。

您可以通過添加一個private集做到這一點(如其他人回答),或者不使用自動屬性:

private int _aip = int.MaxValue; 
public int AIP_NoSet { get {return _aip;}} 

或者,如果你不想設置,只需要使用一個常量:

public const int AIP_NoSet = 2; 
+0

但是,如果我只需要設置,只能得到? – Sergey 2011-01-28 22:15:37

4

你應該寫

public int AIP { get; set; } 

當你寫{ ;}之後,這被視爲用戶實現的屬性的語法錯誤嘗試。你不能沒有setter,但你可以使setter私有:

public int AIP_PrivateSet { get; private set; } 
2

使setter訪問專用並修復語法。

public int AIP_NoSet { get; private set; }