我有一個列表,我想爲包含其內容的集合提供只讀訪問權限。我怎樣才能做到這一點?C#:列表對象的不可變視圖?
喜歡的東西:
public ICollection<Foo> ImmutableViewOfInventory() {
IList<Foo> inventory = new List<Foo>();
inventory.add(new Foo());
return inventory.ImmutableView();
}
此外,一個不可變IEnumerable
也將被罰款。
更新:我現在意識到列表的不可變視圖實際上會更好。 (保留列表排序語義。)
這不會給我列表的行爲,右:
public ReadOnlyCollection<PickUp> InventoryItems()
{
return new ReadOnlyCollection<PickUp>(inventory);
}
我要找的文件中,但不會立即看到ReadOnlyList<T>
。
你想要列表的不可變視圖,還是列表中一組不可變項目的不可變視圖? – adrianbanks 2010-04-15 23:24:16