Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need IEnumerator and IEnumerable?

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.

like image 382
Win Coder Avatar asked Nov 29 '22 01:11

Win Coder


2 Answers

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.

like image 95
Eric Lippert Avatar answered Dec 08 '22 17:12

Eric Lippert


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.

like image 39
jwg Avatar answered Dec 08 '22 16:12

jwg