0

I have been delved in C++ world for a while, but now I'm in .NET world again, VB and C# and I wondered if you have a class that represents a collection of something, and you want the ability to use this in a foreach loop, etc... is it better to implement IEnumerable and IEnumerator yourself or should you inherit from the List<T> where T is the object type in it's singular form?

I know in C++ for example, inheriting from a container is considered a bad idea.

But what about .NET.

EDIT:

It seems my question was slightly misunderstood. I am not unhappy at all with existing collections in .NET. Here is my problem, I have a class called 'Person' and I need a collection called 'Scouts', which I want in an Class called 'Scouts', at this point I'd like to be able to write

foreach Person in Scouts ...

What is the best way to get this Scouts as a collection of People, and be able to use it in a foreach loop?

2
  • Scouts would be the name of a variable or property, not type. Therefore, you'll be perfectly ok declaring public List<Person> Scouts { get; set; }, and then going foreach( var person in Scouts ) ... Commented Mar 5, 2011 at 23:50
  • Also, as a side note, please observe that when you update your original post, nobody gets notified about it. Therefore, nobody will generally come back to you to answer your update. Commented Mar 5, 2011 at 23:52

4 Answers 4

3

You said:

I have a class called 'Person' and I need a collection called 'Scouts', which I want in an Class called 'Scouts', at this point I'd like to be able to write

foreach Person in Scouts ...

If I understand you correctly, you want:

class Scouts : IEnumerable<Person>
{
    private List<Person> scouts;

    public IEnumerator<Person> GetEnumerator()
    {
        return scouts.GetEnumerator();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't understand why List<Person> Scouts would not have worked
@Asher: I got the idea that the Scouts class was going to hold more information than just the list. Otherwise, you're right: just a List<Person> Scouts would work fine.
1

In .NET it is rare to implement IEnumerable<T> or derive from List<T>. If you need a dynamic length collection with indexer you could use List<T> directly and if you need a fixed length collection you could use an array T[]. The System.Collections.Generic namespace contains most of the generic collection classes.

1 Comment

Agreed, having to implement your own collection type is a rarity. There are so many built-in collection types built for most purposes, that you shouldn't need to worry about it. Also, if you do have your own collection type, it is probably backed by a built-in that you can delegate to. This question is purely theoretic at this point I think.
1

The real question is, why would you want to implement your own "collection of something"? That is, why exactly are you not satisfied with existing collections?

Once you answer that question (and are still sure you want to do it), you'd be better off inheriting from one of collections in the System.Collections.ObjectModel namespace - namely, Collection<T>, KeyedCollection<T>, or ReadOnlyCollection<T>, depending on your particular needs.

Comments

0

Tony, as usual 'it depends'. The basic difference between IEnumerable and IList is the ability to add and sort to an IList.

As Darin said, it's more common to just use the classes directly rather than implementing the interfaces at all, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.