Pregunta

Ok so I was just working through the IEnumerator and IEnumerable. Now the MSDN says that the main objective of these things is to iterate through a customer collection.

Fair enough I was however able to iterate through a custom collection without the use of either (or at least I'd like to think so)

namespace ienumerator
{

class person
{
    public person(string first, string second)
    {
        fname = first;
        lname = second;
    }
    public string fname { get; set; }
    public string lname { get; set; }
}

class person_list
{
    public person[] list;
    public person_list(person[] per)
    {
        list = new person[per.Length];
        for (int i = 0; i < per.Length; i++)
        {
            list[i] = per[i];
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        person[] names = new person[3]
        {
            new person("some","one"),
            new person("another","one"),
            new person("sss","ssdad"),
        };

        person_list list_of_names = new person_list(names);
        int i = 0;
        while (i < list_of_names.list.Length)
        {
            Console.WriteLine(list_of_names.list[i].fname);
            i++;
        }
        Console.Read();
    }
 }
}

So what is flawed in my understanding, the above is giving me the same result that I achieve after implementing IEnumerator and IEnumerable.

¿Fue útil?

Solución

I was however able to iterate through a custom collection without the use of either

Of course you can iterate your own collection without the interface; if you couldn't, then how would you implement the interface?

You, as a code implementor, do not implement an interface for your convenience. You implement it for the convenience of the consumer of your code. The interface means "attention customer: you can enumerate the contents of this collection without having to know anything about how I implemented it".

That's the purpose of an interface: it provides an abstraction that allows consumers of your code to use it without having to understand how it works.

Otros consejos

In your main function you used two aspects of person_list to iterate through the list of people, Length and the [] operator for taking the member of a collection at a certain position.

However, what if you had some container or custom collection which didn't implement one or both of these? For example, a class that read a file line by line might not know in advance what the length of the collection was and so not define Length. An enumerator that reads messages from a queue might only be able to take the next one, and not one, say, four positions ahead with [4]. In both cases you can still 'iterate' through the collection properly.

IEnumerator and IEnumerable require exactly the attributes that are needed for foreach to work and no others, which is why functions can use them as requirements, knowing that they will be able to iterate through whatever type of collection they get passed.

You can of course iterate over a custom collection without implementing the IEnumerable interface, but then the iterating code has to know your custom collection.

If you implement IEnumerable, the code that iterates over all elements does not need to know your person_list class. It uses an instance of some class that implements IEnumerable, and it will still work if you exchange the actual instance for something else that also implements IEnumerable later on.

Actually, in C#, it is not required to implement the IEnumerable and IEnumerator to customize you own collections. If you iterate the collection by using traditional way that is fine for you, but it will be trouble if you use foreach to iterate the collection.

Another reason, if you want to expose your class so that VB.NET user can employ you should consider to implement the both interface.

Be note that IEnumerable is difference in non-generic and generic collection. The non-generic one belong to System.Collection the other is belong to System.Collection.Generic namespace

There is a plenty of ways to iterate over custom sequence. One way to do this is to use Iterator Design Pattern.

Iterator Pattern is used to unify traversing over custom sequence (it could be a collection in memory, generated collection or something else). In .NET world this pattern implemented in the following way: we have a iterable object that implements IEnumerable interface (and this shows to any customer of your class that you have a ability for traversing your content).

And we have a iterator itself (object that implements IEnumerator interface) that you can treat as a "pointer" to current position in your iterable object. Because you may have inner loops (i.e. multiple iterations over the same object in one time) you have to split iterable object from the iterator.

Basically there is a plenty of ways to traverse a specific sequence, but Iterator Pattern gives you an ability to "generalize" this approach hiding particular implementation of the traversing algorithm from the callers inside the implementation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top