Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:37:09+00:00 2026-06-17T08:37:09+00:00

Possible Duplicate: Implementing C# IEnumerable<T> for a LinkedList class After searching the web for

  • 0

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; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:37:10+00:00Added an answer on June 17, 2026 at 8:37 am

    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; }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Interface vs Base class A class implementing an interface has to implement
Possible Duplicate: Cannot access private member in singleton class destructor I'm implementing a singleton
Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a
Possible Duplicate: Understanding Threads I have a public class that is implementing a Runnable
Possible Duplicate: Facebook's horizontal scrollbar in ticker & chat sidebar I am implementing a
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Can main function call itself in C++? I found this problem very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.