Differences between IEnumerable and IEnumerator in the iterator, and between the iterator and pointer
The first is the definition of IEnumerable and IEnumerator:
1. The IEnumerable interface allows the use of foreach loops, including the GetEnumerator () method, which can iterate items in the set.
2. the IEnumerator interface is a real set accesser that contains the MoveNext () method and Current attributes. In the foreach loop, if MoveNext () returns True, the Current attribute of the IEnumerator interface is used to obtain a reference of the object for the foreach loop.
3. If you want to iterate a class, you can use GetEnumerator (). Its return type is IEnumerator.
To iterate a class member, use IEnumerable.
The following example uses IEnumerable to iterate the class member Ages in the Person class. The second example is to iterate a class, so IEnumerator is used as the return value.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. collections; namespace _ 10_5_5 {public class person {private string name; private int age; public string Name {get {return name;} set {name = value ;}} public int Age {get {return age;} set {age = value ;}} public person (string PName, int PAge) {Name = PName; Age = PAge ;} public static bool operator> (person a, person B) {if (. age> B. age) return true; else return false;} public static bool operator <(person a, person B) {if (. age> B. age) return false; else return true;} public static bool operator >=( person a, person B) {if (. age> = B. age) {return true;} else return false;} public static bool operator <= (person a, person B) {if (. age <= B. age) return true; else return false;} public class People: DictionaryBase {public IEnumerable Ages // note that IEnumerable {get {foreach (object person in Dictionary. values) {yield return (person as person ). age ;}} public person [] GetOldest () {People oldPeople = new People (); person oldPerson = null; person currentPerson; foreach (DictionaryEntry myPeople in Dictionary) {currentPerson = myPeople. value as person; if (oldPerson = null) {oldPerson = currentPerson; oldPeople. add (oldPerson);} else {if (currentPerson> oldPerson) {oldPeople. clear (); oldPeople. add (currentPerson); oldPerson = currentPerson;} else {if (currentPerson> = oldPerson) {oldPeople. add (currentPerson) ;}}} person [] oldestPeopleArray = new person [oldPeople. count]; int copyIndex = 0; foreach (DictionaryEntry p in oldPeople) {oldestPeopleArray [copyIndex] = p. value as person; copyIndex ++;} return oldestPeopleArray;} public void Add (person p) {Dictionary. add (p. name, p);} public person this [string SName] {get {return (person) Dictionary [SName] ;}set {Dictionary [SName] = value ;}}} class Program {static void Main (string [] args) {person a = new person ("Jack", 11); person B = new person ("Json", 10 ); people s = new People (); s. add (a); s. add (B); foreach (int age in s. ages) {Console. writeLine ("{0} \ t", age);} Console. readKey ();}}}
The following is an example of a custom iterator:
Primer. CS
Using System; using System. collections; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Ch11Ex03_Exam {public class Primes {private long min; private long max; public Primes (): this (2,100) {} public Primes (long minNum, long maxNum) {if (minNum <2) {min = 2;} else {min = minNum;} max = maxNum;} public IEnumerator GetEnumerator () // IEnumerator {for (long I = min; I <max; I ++) {int flag = 1; for (long j = 2; j <Math. sqrt (min); j ++) {if (I % j = 0) {flag = 0; break ;}} if (flag = 1) {yield return I ;}}}}}
Program. CS:
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Ch11Ex03_Exam{ class Program { static void Main(string[] args) { Primes s = new Primes(2, 100); foreach(long i in s) { Console.WriteLine("{0}\t", i); } Console.ReadKey(); } }}