SoFunction
Updated on 2025-03-07

Example analysis of IEnumerable interface usage example in C#

This article describes the usage of the IEnumerable interface in C#. Share it for your reference. The specific analysis is as follows:

Enumerations can be used to read data in a collection, but cannot be used to modify the underlying collection.

Initially, the enumeration is positioned before the first element in the collection. The Reset method also returns the enumeration to this position. At this location, the Current property is not defined. Therefore, before reading the value of Current, the MoveNext method must be called to advance the enumeration to the first element of the collection.

Current returns the same object before calling MoveNext or Reset. MoveNext Set Current to the next element.

If MoveNext passes the end of the collection, the enumeration is placed after the last element in the collection, and MoveNext returns false. When the enumeration is in this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returns false, Current is undefined. To set Current again as the first element of the collection, you can call Reset and then call MoveNext.

As long as the set remains unchanged, the enumeration remains valid. If a change is made to the collection (such as adding, modifying, or deleting elements), the enumeration will be invalid and unrecoverable, and its behavior is uncertain.

Enumerations do not have exclusive access to a collection; therefore, enumerating a collection from beginning to end is not essentially a thread-safe process. To ensure thread safety during the enumeration process, you can lock the collection throughout the enumeration process. To allow multiple threads to access the collection for read and write operations, you must implement your own synchronization.

The following code example demonstrates how to implement the IEnumerable interface for a custom collection. In this example, there is no explicit call but the GetEnumerator is implemented in order to support the use of foreach (For Each in Visual Basic). This code example is taken from a larger example of the IEnumerable interface.

using System;
using ;
public class Person
{
 public Person(string fName, string lName)
 {
   = fName;
   = lName;
 }
 public string firstName;
 public string lastName;
}
public class People : IEnumerable
{
 private Person[] _people;
 public People(Person[] pArray)
 {
  _people = new Person[];
  for (int i = 0; i < ; i++)
  {
   _people[i] = pArray[i];
  }
 }
 IEnumerator ()
 {
  return (IEnumerator) GetEnumerator();
 }
 public PeopleEnum GetEnumerator()
 {
  return new PeopleEnum(_people);
 }
}
public class PeopleEnum : IEnumerator
{
 public Person[] _people;
 // Enumerators are positioned before the first element
 // until the first MoveNext() call.
 int position = -1;
 public PeopleEnum(Person[] list)
 {
  _people = list;
 }
 public bool MoveNext()
 {
  position++;
  return (position < _people.Length);
 }
 public void Reset()
 {
  position = -1;
 }
 object 
 {
  get
  {
   return Current;
  }
 }
 public Person Current
 {
  get
  {
   try
   {
    return _people[position];
   }
   catch (IndexOutOfRangeException)
   {
    throw new InvalidOperationException();
   }
  }
 }
}
class App
{
 static void Main()
 {
  Person[] peopleArray = new Person[3]
  {
   new Person("John", "Smith"),
   new Person("Jim", "Johnson"),
   new Person("Sue", "Rabon"),
  };
  People peopleList = new People(peopleArray);
  foreach (Person p in peopleList)
   ( + " " + );
 }
}
/* This code produces output similar to the following:
 *
 * John Smith
 * Jim Johnson
 * Sue Rabon
 *
 */

I hope this article will be helpful to everyone's C# programming.