I have a list, and I want to provide read-only access to a collection containing its contents. How can I do this?
Something like:
public ICollection<Foo> ImmutableViewOfInventory() {
IList<Foo> inventory = new List<Foo>();
inventory.add(new Foo());
return inventory.ImmutableView();
}
Additionally, an immutable IEnumerable
would also be fine.
UPDATE: I realize now that an immutable view of the list would actually be better. (Preserving list ordering semantics.)
This won't give me list behavior, right:
public ReadOnlyCollection<PickUp> InventoryItems()
{
return new ReadOnlyCollection<PickUp>(inventory);
}
I'm looking in the documentation but not immediately seeing ReadOnlyList<T>
.