Ienumerable Interface
Public enumeration number, which supports simple iteration on a non-generic set.
Namespace: system. Collections
Public class person {Public String firstname; Public String lastname; Public Person (string fname, string lname) {This. firstname = fname; this. lastname = lname;} public class people: ienumerable {private person [] _ person; Public people (person [] parray) {_ person = new person [parray. length]; for (INT I = 0; I <parray. length; I ++) _ person [I] = parray [I];} public peopleenum getenumerator () // return peopleenum current instance {return New peopleenum (_ person );} ienumerator ienumerable. getenumerator () {return (ienumerator) getenumerator () ;}} public class peopleenum: ienumerator {int position =-1; public person [] _ person; public peopleenum (person [] list) {_ person = List;} public person current {get {try {return _ person [position];} catch (indexoutofrangeexception) {Throw new invalidoperationexception () ;}} object ienumerator. current {get {Return Current;} public bool movenext () {position ++; Return (position <_ person. length) ;}public void reset () {position =-1 ;}}
Test:
static void Main(string[] args) { 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) Console.WriteLine((p.firstName + " " + p.lastName)); Console.ReadKey(); }
The effect is as follows: