Implementing IEnumerable to my object

c#, ienumerable, ienumerator

Solution

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; }
        }
    }
}

Problem

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; } } ```

Original source

Related problems