Sunday, August 14, 2011

How to Inherit from System.Collections.Generic.List

Problem:
I want to extend a few of the properties on the SystemCollections.Generic.List however I am not sure how to inherit from that object. Please help.


Solution:
System.Collections.Generic.List is a generic collection that does not contain any virtual members. This was designed for performance not for inheritance. So this can't inherit.

The following generic collections are designed for inheritance and should be exposed instead of System.Collections.Generic.List.
  1. System.Collections.ObjectModel.Collection
  2. System.Collections.ObjectModel.ReadOnlyCollection
  3. System.Collections.ObjectModel.KeyedCollection 
You could make your list classes implement the IList<of T> and extend the methods by embedding a call to the generic list's method in your class' method.





No comments:

Post a Comment