Read-only Wrapper for a Collection [C#]
This example shows how to easily create a read-only wrapper for a collection. This is useful if you have a private collection (with full access) and you need to show it as a read-only collection for the public.
In the example bellow, there is a class with private collection
List<int> _items
and public property IList<int>
Items
which is the readonly wrapper to the private collection.
public class MyClass { private List<int> _items = new List<int>(); public IList<int> Items { get { return _items.AsReadOnly(); } } }
See also
- [C#] Sorting Arrays – how to sort arrays of custom type
- List<T> – MSDN – list collection generic class
- IList<T> – MSDN – list collection generic interface
- List<T>.AsReadOnly – MSDN – returns a read-only IList<T> wrapper for the List<T> collection