在一類,CharList,我有一個私人表(表<名單<NewChar> >)和一對索引的:自定義索引器「不能分配給」?
private List<List<NewChar>> _charList;
// ...
public NewChar this[Int32 inner, Int32 outer]
{
get
{
if (inner < 0 || inner >= _charList.Count ||
outer < 0 || outer >= _charList[inner].Count)
throw new ArgumentException("NewCharList indexer[,]: Invalid index!");
return _charList[inner][outer];
}
}
public List<NewChar> this[Int32 index]
{
get
{
if (index < 0 || index > MaxCharListIndex)
throw new ArgumentException("NewCharList indexer[]: Invalid index");
List<NewChar> ret = new List<NewChar>(_charList[index].Count);
for (int i = 0; i < _charList[index].Count; i++)
ret.Add(_charList[index][i]);
return ret;
}
}
在測試代碼(其他類),如果我叫
charList[0] = null;
我得到一個編譯錯誤「屬性或索引XXX不能被分配到 - 它是隻讀的」,但如果我叫
charList[0][0] = new NewChar(22,22);
編譯器將允許它,但值不會改變。 爲什麼會讓我分配給第二個? 我不能爲了我的生活而弄明白,這讓我發瘋。當你寫這個(即使它不改變的值)
對不起 - 我忘記提及這是我想要的行爲(索引者不改變這個列表) - 只是第二個會,我想知道爲什麼。 – Richard