What are the advantages/disadvantages of inheriting collection vs. implementing iEnumerable()
What are the advantages/disadvantages of inheriting collection vs. implementing iEnumerable()
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s usually a mistake (IMO) to derive a class from one of the collection types. I would do it to create a new general purpose collection type – but if the class has some “business” purpose, and just happens to act like a collection in other ways, I think it’s not a good idea to derive from a collection class.
If you implement
IEnumerable<T>you’re effectively giving the message that your class can be used as a sequence ofT(probably for a particular, concreteT) but there’s more to it than that: it has non-collection abilities too. Of course, you may not even need to implement it yourself – you could have properties or methods which return an appropriate sequence. It depends on the use case, really.Don’t forget that you only get to derive from one base class in .NET… so think very carefully before you use up that one chance on a collection type.