Monday, July 14, 2008

I blogged recently about using public Collection instead of public List. The Visual Studio Code Analysis Team explained in their blog how to re-use List methods in classes derived from Collection<T> generic class. As you can see, their approach requires your class constructor to call base constructor of List<T>. To make developer's life easier, I proposed to make a generic ListCollection<T> class with such a constructor and inherit custom collection classes from it.Then I read The Missing .NET #2: Collection<T> AddRange() article by Jason Kemp. He implemented "missing" methods of Collection<T> as extension methods. I added the same methods to my ListCollection<T> class. Because a default constructor of ListCollection<T> class guarantees that its protected Items property actually contains a List<T>, it is easier to implement "missing" methods; you just call corresponding methods of List<T>:

///<summary>

/// A generic Collection class which ensures that it actually contains generic List.

/// This provides us with an ability to re-use any public method of List&lt;T&gt; with a simple shell function.

/// Such a shell could be implemented in a concrete implementation of this generic class,

/// as extensions methods for ListCollection&lt;T&gt; or directly in ListColection&lt;T&gt; as it is done below.