1. using System;
  2. using System.Collections;
  3. using System.Windows.Forms;
  4.  
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public class Person
  9.     {
  10.         public Person(string fName, string lName)
  11.         {
  12.             this.firstName = fName;
  13.             this.lastName = lName;
  14.         }
  15.  
  16.         public string firstName;
  17.         public string lastName;
  18.     }
  19.  
  20.     public class People : IEnumerable
  21.     {
  22.         private Person[] _people;
  23.         public People(Person[] pArray)
  24.         {
  25.             _people = new Person[pArray.Length];
  26.  
  27.             for (int i = 0; i < pArray.Length; i++)
  28.             {
  29.                 _people[i] = pArray[i];
  30.             }
  31.         }
  32.  
  33.         IEnumerator IEnumerable.GetEnumerator()
  34.         {
  35.             return (IEnumerator)GetEnumerator();
  36.         }
  37.  
  38.         public PeopleEnum GetEnumerator()
  39.         {
  40.             return new PeopleEnum(_people);
  41.         }
  42.     }
  43.  
  44.     public class PeopleEnum : IEnumerator
  45.     {
  46.         public Person[] _people;
  47.  
  48.         // Enumerators are positioned before the first element
  49.         // until the first MoveNext() call.
  50.         int position = -1;
  51.  
  52.         public PeopleEnum(Person[] list)
  53.         {
  54.             _people = list;
  55.         }
  56.  
  57.         public bool MoveNext()
  58.         {
  59.             position++;
  60.             return (position < _people.Length);
  61.         }
  62.  
  63.         public void Reset()
  64.         {
  65.             position = -1;
  66.         }
  67.  
  68.         object IEnumerator.Current
  69.         {
  70.             get
  71.             {
  72.                 return Current;
  73.             }
  74.             set
  75.             {
  76.                 _people[position] = null;
  77.             }
  78.         }
  79.  
  80.  
  81.         public Person Current
  82.         {
  83.             get
  84.             {
  85.                 try
  86.                 {
  87.                     return _people[position];
  88.                 }
  89.                 catch (IndexOutOfRangeException)
  90.                 {
  91.                     throw new InvalidOperationException();
  92.                 }
  93.             }
  94.             set
  95.             {
  96.                 _people[position] = value;
  97.             }
  98.         }
  99.     }
  100.  
  101.     public class badClass
  102.     {
  103.         public badClass(ref Person prs)
  104.         {
  105.             prs = new Person("Bai", "Hui");
  106.         }
  107.     }
  108.  
  109.     public partial class Form1 : Form
  110.     {
  111.         public Form1()
  112.         {
  113.             InitializeComponent();
  114.         }
  115.  
  116.         private void button1_Click(object sender, EventArgs e)
  117.         {
  118.             Person[] peopleArray = new Person[3]
  119.             {
  120.                 new Person("John", "Smith"),
  121.                 new Person("Jim", "Johnson"),
  122.                 new Person("Sue", "Rabon"),
  123.             };
  124.  
  125.             People peopleList = new People(peopleArray);
  126.  
  127.             PeopleEnum tmp = peopleList.GetEnumerator();
  128.             while (tmp.MoveNext())
  129.             {
  130.                 badClass baddie = new badClass(ref tmp.Current);
  131.             }
  132.  
  133.             tmp.Reset();
  134.  
  135.             while (tmp.MoveNext())
  136.             {
  137.                 Console.WriteLine(tmp.Current.firstName+" "+tmp.Current.lastName);
  138.             }
  139.         }
  140.     }
  141. }