Click here to Skip to main content
15,844,673 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi there,

I googled out about getting answer for your coding question and i came across codeproject

I have been studying C# Generics.. and i have this piece of code. i am being able to comprehend a point that foreach loop iterates over number of objects but here in this code only one object of CustomList<int> class is created that is list1 and then list1 is used in forloop to add(insert) numbers via Nodes..but how GetEnumerator works here..we had only one object that is list1 that is used over and over in for loop to insert numbers...please guide if I am clearly stating the problem

What I have tried:

C#
namespace GenericsusingLinkedList_25_03
{
    // Test the CustomList
    class TestCustomList
    {
        static void Main()
        {
            // Declare a List of type int, then loop through the List
            CustomList<int> list1 = new CustomList<int>();

            for (int x = 1; x <= 3; x++)
            {
                list1.Add(x);
            }

            foreach (int i in list1)
            {
              System.Console.Write(i + " ");
            }
           
            Console.ReadKey();
        }
    }
    public class CustomList<t>
    {
        // Fields
        private Node head=null;

        // The nested class is also generic on T
        private class Node
        {
            // Fields
            private Node next;

            // T as private member data type
            private T data;

            
            // Properties
            public Node Next
            {
                get
                {
                    return next;
                }
                set
                {
                    next = value;
                }
            }
            // T as return type of the Property
            public T Data
            {
                get
                {
                    return data;
                }
                set
                {
                    data = value;
                }
            }
            // T used in non-generic constructor
            public Node(T pData)
            {
                this.next = null;
                this.data = pData;
            }
        }
         // T as method parameter type:
        public void Add(T pType)
        {
            Node n = new Node(pType);

            n.Next = head;    //next of current node is assigned head of previous node
            this.head = n;    //head of current is same as n
        }
        // Enables foreach on the List
        public IEnumerator<t> GetEnumerator()
        {
            Node current = head;
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }     
    }       
}
Posted
Updated 27-Mar-17 5:33am
v2

1 solution

The key statement is
C#
yield return current.Data;

which allows an iterator to receive each element of a collection one at a time. see yield (C# Reference)[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900