6

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; }
}
3
  • 1
    Here is *not a good tutorial on implementing IEnumerable on a custom class. Here is a better one.
    – NominSim
    Jan 16, 2013 at 14:26
  • Note that, technically, no don't need to implement IEnumerable to use your class with a foreach-loop...
    – sloth
    Jan 16, 2013 at 14:39
  • Yeah, I know but I want to learn how to use it. Jan 16, 2013 at 14:46

2 Answers 2

6

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;
    }
}
3
  • Just to point out, C# helps you out with this a lot if you create the GetEnumerator() method yourself with yield statements etc.
    – NominSim
    Jan 16, 2013 at 14:55
  • I get the: Error 1 Inconsistent accessibility: parameter type 'xx.LL' is less accessible than method 'xx.MyLinkedListEnumerator.MyLinkedListEnumerator(xx.LL)' C:\\xx\Program.cs error Jan 16, 2013 at 15:14
  • @Ben: That's probably because your LL class is private. Mark it public. Jan 16, 2013 at 15:18
4

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; }
        }
    }
}
2
  • Try to compile it now, without the foreach loop and WriteLite. Jan 16, 2013 at 15:02
  • Thanks it works now. Now I gonna study it. Jan 16, 2013 at 15:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.