Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IEnumerable to my object [duplicate]

Possible Duplicate:
Implementing C# IEnumerable<T> for a LinkedList class

After searching the web for some hours now I still can't understand how IEnumerable/IEnumerator works and how to implement it.

I've constructed a simple LinkedList from scratch but now I want to implement IEnumerable for it so I can foreach it. How do I do that?

class Program
{
    LL myList = new LL();

    static void Main()
    {
        var gogo = new Program();
    }
    public Program()
    {

        myList.Add("test");
        myList.Add("test1");

        foreach (var item in myList) //This doesn't work because I havn't implemented Ienumerable
            Console.WriteLine(item);

        Console.Read();
    }
}


class LL
{

    private LLNode first;

    public void Add(string s)
    {
        if (this.first == null)
            this.first = new LLNode() { Value = s };
        else
        {
            var node = this.first;
            while (node.Next != null)
                node = node.Next;

            node.Next = new LLNode() { Value = s };
        }
    }


class LLNode
{
    public string Value { get; set; }
    public LLNode Next { get; set; }
}
like image 507
President Camacho Avatar asked Jan 16 '13 14:01

President Camacho


People also ask

What inherits from IEnumerable?

ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.

What is IEnumerable <> in C#?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

Is IEnumerable a collection?

IEnumerable<T> is an interface that represents a sequence. Now; collections can usually be used as sequences (so... List<T> implements IEnumerable<T> ), but the reverse is not necessarily true. In fact, it isn't strictly required that you can even iterate a sequence ( IEnumerable<T> ) more than once.


2 Answers

It's really not that hard. To implement IEnumerable you just need to implement the GetEnumerator method.

To do that you need to create another class that implements IEnumerator. Implementing IEnumerator is pretty easy. Generally you will pass a reference to your collection when you create the enumerator (in GetEnumerator) and the enumerator will keep track of which item is the current item. Then it will provide MoveNext which just changes the Current to the next item (and returns false if it's at the end of the list) and Reset which just sets the Current back to before the first node.

So in very broad, untested code terms, you need something like:

public class MyLinkedListEnumerator : IEnumerator
{
    private LL myList;
    private LLNode current;

    public object Current
    {
       get { return current; }
    }

    public MyLinkedListEnumerator(LL myList) 
    {
        this.myList = myList;
    }

    public bool MoveNext()
    {
        if (current == null) {
            current = myList.first;
        }
        else {
            current = current.Next;
        }
        return current != null;
    }

    public void Reset() 
    {
        current = null;
    }
}
like image 155
Matt Burland Avatar answered Sep 22 '22 03:09

Matt Burland


What you need to do is:

(1) Make your class implement IEnumerable<T> where T is the type of the enumerated items. (In your case, it looks like it would be LLNode).

(2) Write a public IEnumerator<T> GetEnumerator. Implement it using the "yield" keyword.

(3) Add a IEnumerator IEnumerable.GetEnumerator() method and just return GetEnumerator().

The following code should make this clear. Where I have <int>, you should put <LLNode>, assuming that is the correct type.

using System;
using System.Collections;
using System.Collections.Generic;

namespace Demo
{
    internal class Program
    {
        private static void Main()
        {
            var test = new MyDemo();

            foreach (int item in test)
            {
                Console.WriteLine(item);
            }
        }
    }

    public class MyDemo: IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
            // Your implementation of this method will iterate over your nodes
            // and use "yield return" to return each one in turn.

            for (int i = 10; i <= 20; ++i)
            {
                yield return i;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

I would have modified your code to do it properly, but the code you posted won't compile.

[EDIT]

Now you've updated your code, I can see that you want to enumerate the values. Here's the completed code:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Demo
{
    internal class Program
    {
        private LL myList = new LL();

        private static void Main()
        {
            var gogo = new Program();
        }

        public Program()
        {
            myList.Add("test");
            myList.Add("test1");

            foreach (var item in myList) // This now works.
                Console.WriteLine(item);

            Console.Read();
        }
    }


    internal class LL: IEnumerable<string>
    {
        private LLNode first;

        public void Add(string s)
        {
            if (this.first == null)
                this.first = new LLNode
                {
                    Value = s
                };
            else
            {
                var node = this.first;
                while (node.Next != null)
                    node = node.Next;

                node.Next = new LLNode
                {
                    Value = s
                };
            }
        }

        public IEnumerator<string> GetEnumerator()
        {
            for (var node = first; node != null; node = node.Next)
            {
                yield return node.Value;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private class LLNode
        {
            public string Value { get; set; }
            public LLNode Next { get; set; }
        }
    }
}
like image 29
Matthew Watson Avatar answered Sep 21 '22 03:09

Matthew Watson