In the previous article, the foreach function is implemented for the custom class, but the ienumerator interface (including movenext, reset function, and current attribute) must be implemented in the implementation process so that the getenumerator function can obtain the number of enumerations, it's complicated and complicated.
I already know that the main class can implement the getenumerator function to foreach, while implementing the ienumerator interface is only one of the methods to implement the getenumerator function. Is there another way to implement the getenumerator function? Yes, C #2.0 provides us with a very simple method to implement the getenumerator function, that is, to use the iterator! (Remember, the iterator in C # is different from that in C ++)
The following is an explanation from msdn:
Iterator Overview
An iterator is a piece of code that returns an ordered sequence of values of the same type.
The iterator can be used as a method, operator, orGetThe Code body of the accessors.
Iterator code usageYield returnThe statement returns each element in sequence.Yield breakIteration ends. For more information, see yield.
Multiple iterators can be implemented in the class. Each iterator must have a unique name like any class member andForeachThe statement is called by the client code as follows:Foreach (int x in sampleclass. iterator2 ){}
The return type of the iterator must beIenumerable, Ienumerator,Ienumerable<T> or ienumerator <t>.
The yield keyword is used to specify the returned value. ArrivalYield returnStatement, the current location is saved. The next time you call the iterator, it will start execution again from this position.
Part 1: use the default iterator to obtain the enumerated number
1. Define the person class
View code
1 public class Person
2 {
3 public string Name;
4 public int Age;
5
6 public Person(string name, int age)
7 {
8 Name = name;
9 Age = age;
10 }
11
12 public override string ToString()
13 {
14 return "Name: " + Name + "\tAge: " + Age;
15 }
16 }
2. Define the leleenum1 class and implement the getenumerator function (that is, the iterator, which is the default iterator ), however, the implementation process is much simpler than the implementation process described in the previous article, which is the efficacy of yield. The implementation principle of yield is not detailed here. It can be summarized as follows: the iteration blocks in the getenumerator function encapsulate the movenext, reset method, and current attributes of the ienumerator interface, but the nature remains unchanged, it is easier to use.
1 public class peopleenum1
2 {
3 private person [] _ Perple;
4
5 // Constructor
6 Public peopleenum1 (person [] list)
7 {
8 _ Perple = new person [list. Length];
9 For (INT I = 0; I <list. length; I ++)
10 {
11 _ Perple [I] = list [I];
12}
13}
14
15 public ienumerator getenumerator ()
16 {
17 For (INT I = 0; I <_ Perple. length; I ++)
18 {
19 yield return _ Perple [I];
20}
21}
22
23}
3. Main Function Code
View code
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 Person[] persons = new Person[]
6 {
7 new Person("aaa", 20),
8 new Person("bbb", 21),
9 new Person("ccc", 22)
10 };
11
12 PeopleEnum1 peopleEnum = new PeopleEnum1(persons);
13
14 foreach (var item in peopleEnum)
15 {
16 System.Console.WriteLine(item);
17
18 }
19
20 System.Console.ReadKey();
21 }
22 }
Run, succeeded, hey
Part 2: Custom iterator
The first part uses the default iterator getenumerator to implement the class foreach. We can also define our own iterator to get the desired enumerated number. By comparison, I want to list the information of minors. The default iterator is powerless. How can I implement a custom iterator?
1. Add an iterator getchildren for the peopleenum1 class. Here, this iterator is an attribute and can also be defined as a function. For elements in the class, only the elements with age less than 18 are yield return. Others are not.
View code
2. Main Function Code
View code
Output result:
As you can see, the custom iterator getchildren succeeded foreach.
Note: The default return type of the getenumerator iterator is ienumerator.
The class name is directly followed. The returned type of the custom iterator getchildren is ienumerable, and after using foreach
Peopleenum. getchildren (class name. iterator name ). It seems that the principle is not clear.
Reference from: http://www.cnblogs.com/zouzf/archive/2012/02/22/2362954.html