pistacchio
pistacchio

Reputation: 58953

Inheriting from List<T>

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.

Upvotes: 52

Views: 44255

Answers (7)

MSBassSinger
MSBassSinger

Reputation: 1

Keeping in mind that if you want all the functionality of List<T>, which is:

public class List<T> : ICollection<T>, IEnumerable<T>, IEnumerable, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection, IList

Then your class can inherit those.

However, if your class is not part of an API that could be serialized, List<T> is suitable.

public class MyItems : List<MyItem>
{
    public MyItems() : base()
    {
    }
}

Upvotes: -1

Jo&#227;o Angelo
Jo&#227;o Angelo

Reputation: 57718

If you want to create a publicly exposed animal collection you should not inherit from List<T> and instead inherit from Collection<T> and use the postfix Collection in the class name. Example: AnimalCollection : Collection<Animal>.

This is supported by the framework design guidelines, more specifically:

DO NOT use ArrayList, List<T>, Hashtable, or Dictionary<K,V> in public APIs. Use Collection<T>, ReadOnlyCollection<T>, KeyedCollection<K,T>, or CollectionBase subtypes instead. Note that the generic collections are only supported in the Framework version 2.0 and above.

Upvotes: 53

Euphoric
Euphoric

Reputation: 12849

Deriving from List<T> is not advised. Primarily because List was never meant for extension, but for performance.

If you want to create your own specific collection, you should inherit from Collection<T>. In your case it would be:

class Animals : Collection<Animal> {}

Upvotes: 14

Anderson Imes
Anderson Imes

Reputation: 25650

Constructors are not inherited along with the class. You have to reimplement your desired constructors.

public class AnimalsCollection : List<Animal>
{
     public AnimalsCollection(IEnumerable<Animal> animals) : base(animals) {} 
}

Upvotes: 32

Adam Houldsworth
Adam Houldsworth

Reputation: 64537

Bear in mind that inheriting from List isn't as fully featured as you may need, a lot of the members are not virtual so the only way of covering the base implementation is to shadow it with the new syntax (as opposed to override).

If you need to start exposing custom behaviour on standard list actions, I would implement all the list interfaces on a type that simply uses an inner list for the actual storage.

This is heavily dependent on your final requirements.

Upvotes: 6

Daniel Earwicker
Daniel Earwicker

Reputation: 116744

It isn't necessary to inherit from List to use collection initialization syntax or use Linq extension methods.

Just implement IEnumerable and also an Add method.

Upvotes: 4

Tokk
Tokk

Reputation: 4502

class Animals : List<Animal> {}

seemes the best way, because you can use it just after defining it like this.

So my advice is your questions title: Inherit List<T> ;-)

Upvotes: 3

Related Questions