PersonCollection persons = new PersonCollection ( new Person[]{new Person("Larry")});
foreach (Person person in persons)
{
Console.WriteLine(person.Name);
}
foreach (Person person in persons)
{
Console.WriteLine(person.Name);
}
}
}
public class Person {
public string Name { get; set; }
public Person(string name)
{
this.Name = name;
}
}
public class PersonCollection : IEnumerable,IEnumerator
{
private Person[] _peoples;
private int position = -1;
public PersonCollection(Person[] list)
{
_peoples = list;
}
public bool MoveNext()
{
position++;
return (position < _peoples.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _peoples[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public IEnumerator GetEnumerator()
{
return new PersonCollection(_peoples);
}
}</pre>