您的聲明或數組在語法上不正確。
public class AdditionsFileExport: IFileExport
{
// "wwwe", "sdd", "e3rs" will be default values
private string[] fileHeaders = new[] { "wwwe", "sdd", "e3rs" };
public string[] FileHeaders
{
get { return fileHeaders; }
set { fileHeaders = value; }
}
}
如果需要只讀屬性,刪除setter和與readonly
關鍵字聲明fileHeaders
。
注意,這:
public class AdditionsFileExport: IFileExport
{
public string[] FileHeaders
{
get { return new[] { "wwwe", "sdd", "e3rs" }; }
}
}
不是很長的路要走,因爲每次你打電話FileHeaders
時間,創建數組的新實例。因此,這種說法會失敗:
var foo = new AdditionsFileExport();
Debug.Assert(foo.FileHeaders == foo.FileHeaders);
還要注意,數組不是一般的好返回類型,因爲你不能改變屬性的實現,例如,像這樣:
public class AdditionsFileExport: IFileExport
{
private readonly fileHeaders = new List<string>
{
"wwwe", "sdd", "e3rs"
};
public string[] FileHeaders
{
// won't compile, because List<> isn't array
get { return fileHeaders; }
}
}
IEnumerable<string>
會是一個更好的選擇。
''''''中的部分應該是一個指定大小的整數,或者留空和提供的值(在'{''}'中)。 – crashmstr
由於你的get只返回某些值,你根本不需要set。否則,您需要將兩者都更改爲使用備份變量。 – crashmstr
丹尼斯答案是正確的。順便說一下,你不應公開暴露數組。如果你已經啓用了代碼分析,那麼你會得到警告, –