Collections vs. IEnumerable

What are the advantages / disadvantages of collection inheritance versus iEnumerable () implementation

+4
source share
2 answers

Usually an error (IMO) infers a class from one of the collection types. I would do this to create a new type of general-purpose collection, but if the class has a specific β€œbusiness goal” and just happens, as in other cases, like in a collection, I think it’s not a good idea to get a collection class .

If you implement IEnumerable<T> , you are effectively communicating that your class can be used as a sequence of T (perhaps for a specific, specific T ), but there is more than that: it has non-collectible capabilities too. Of course, you may not need to implement it yourself - you may have properties or methods that return the appropriate sequence. It really depends on the option used.

Remember that you only get one base class in .NET ... so think carefully before using this chance for a collection type.

+4
source

Implementation of IEnumerable is practically not required. There is almost always a suitable collection that you can use.

For me, List<T> and Dictionary<Tkey,TValue> cover 90% of what I usually need to do.

+1
source

All Articles