02 |
using System.Collections.Generic; |
05 |
using System.Collections; |
07 |
namespace IEnumeratorSample |
09 |
class Person : IEnumerable |
13 |
public Person( string name, string age) |
20 |
public Person(Person[] array) |
22 |
per = new Person[array.Length]; |
24 |
for ( int i = 0; i < array.Length; i++) |
30 |
public IEnumerator GetEnumerator() |
32 |
return new PersonEnum(per); |
36 |
class PersonEnum : IEnumerator |
41 |
public PersonEnum(Person[] list) |
46 |
public bool MoveNext() |
49 |
return (position < _per.Length); |
63 |
return _per[position]; |
65 |
catch (IndexOutOfRangeException) |
67 |
throw new InvalidOperationException(); |
75 |
static void Main( string [] args) |
77 |
Person[] per = new Person[2] |
79 |
new Person( "guojing" , "21" ), |
80 |
new Person( "muqing" , "21" ), |
83 |
Person personlist = new Person(per); |
84 |
foreach (Person p in personlist) |
85 |
Console.WriteLine( "Name is " + p.Name + " and Age is " + p.Age); |
|