What is the fastest way to implement a new class that inherits from List<T>
?
class Animal {}
class Animals : List<Animal> {} // (1)
One problem I've encountered: By simply doing (1), I've found that I'm not getting the benefit of inheriting any constructors from List<T>
.
In the end, I'd like Animals
to behave a lot like a List<T>
(e.g., can be constructed, compatibility with Linq). But in addition, I'd also like to be able to add my own custom methods.
Best Answer
If you want to create a publicly exposed animal collection you should not inherit from
List<T>
and instead inherit fromCollection<T>
and use the postfixCollection
in the class name. Example:AnimalCollection : Collection<Animal>
.This is supported by the framework design guidelines, more specifically: