Search code examples
c#foreachienumerable

An example for supporting foreach without implementing IEnumerable


I am looking at this blog which explains that foreach can be supported without implementing IEnumerable. But does not go into the details of the implementation.

I am looking for an example of how to support foreach without implementing IEnumerable.


Solution

  • Thanks to @Sam I am 's comment, I am able to tweak the code on this page and put together the following, without using IEnumerable or IEnumerator:

       //Person Object
       public class Person
       {
            public Person(string fName, string lName)
            {
                this.firstName = fName;
                this.lastName = lName;
            }
        string firstName;
        public string lastName;
       }
    
       //****Object Collection. 
       //****This class usually needs to implement IEnumerable 
       //****But we are avoiding that here.
       public class People
       {
          private Person[] _people;
          public People(Person[] pArray)
            {
               _people = new Person[pArray.Length];    
               for (int i = 0; i < pArray.Length; i++)
               {
                  _people[i] = pArray[i];
               }
             }
    
        public PeopleEnumSimulator GetEnumerator()
        {
            return new PeopleEnumSimulator(_people);
        }
    
        public class PeopleEnumSimulator
        {
            public Person[] _people;
    
            int position = -1;
    
            public PeopleEnumSimulator(Person[] list)
            {
                _people = list;
            }
    
            public bool MoveNext()
            {
                position++;
                return (position < _people.Length);
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            public Person Current
            {
                get
                {
                    try
                    {
                        return _people[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }
    }   
    
    
    //****Now, Use the Foreach
        Person[] myPeople = new Person[3]
        {
            new Person("John", "Smith"),
            new Person("Jim", "Johnson"),
            new Person("Sue", "Rabon"),
        };
        People peopleList = new People(myPeople);
        foreach (Person p in peopleList)
            Response.Write(p.firstName + " " + p.lastName);
    
    
    
    
    
    //****************************************
    //******** A Generic Implementation********
    public class People<T>
    {
        private T[] _people;
        public People(T[] pArray)
        {
            _people = new T[pArray.Length];
    
            for (int i = 0; i < pArray.Length; i++)
            {
                _people[i] = pArray[i];
            }
        }
    
        public PeopleEnumSimulator GetEnumerator()
        {
            return new PeopleEnumSimulator(_people);
        }
    
        public class PeopleEnumSimulator
        {
            public T[] _people;
    
            int position = -1;
    
            public PeopleEnumSimulator(T[] list)
            {
                _people = list;
            }
    
            public bool MoveNext()
            {
                position++;
                return (position < _people.Length);
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            public T Current
            {
                get
                {
                    try
                    {
                        return _people[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }
    }
    
           // use the foreach
           //For Type Person
                People<Person> peopleList = new People<Person>(myPeople);
    
                foreach (Person p in peopleList)
                    Response.Write(p.firstName + " " + p.lastName);
    
                //break
                Response.Write(" </br></br></br>       ");
    
                //For Type Int
                int[] n3 = { 2, 4, 6, 8 };
    
                People<int> intList = new People<int>(n3);
    
                foreach (int p in intList)
                    Response.Write(p);