March 6, 2008

AsReadyOnly method

The AsReadyOnly method. This method is exposed by the List, Set, Array<>, and others and generates what is basically an adapter to your collection. The return value is a System.Collections.ObjectModel.ReadOnlyCollection that is the best of both worlds , it shows live data from the internal collection and it doesn't let the consumer modify the collection contents. Here's an example of the implementation
public class MyManagerClass
{
     private List _objects = new List();

     public ReadOnlyCollection Objects
     {
          get { return _objects.AsReadOnly(); }
     }
}

No comments: