Jul 1, 2012

Create ReadOnly wrapper for Collection

1 comment

How to Create ReadOnly Wrapper For Collection?


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 below, 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.

Code:
public class TestClass
{
    private List _items = new List();

    public IList Items
    {
        get { return _items.AsReadOnly(); }
    }
}

1 comment :