Good evening, everyone. The key to simplifying iterators is to talk about the yield keyword today in conjunction with MSDN in the book.
If you use the yield keyword in a statement, it means that the method, operator, or get accessor that it appears in is an iterator, and by using the yield definition iterator, you do not need to display the class when implementing the IEnumerable and IEnumerator modes of the custom collection type ( Keep the enumeration state Class), there are two forms of using yield, as follows
1 yield return An expression 2 yield Break
Let's start with the yield return statement, which returns an element each time. Use the iterator method with a foreach statement or a LINQ query. Each iteration of the Foreach loop invokes the iterator method, saving the state that is currently returned.
1 static void Main(string[] args)
2 {
3 foreach (int i in GetValues())
4 {
5 Console.WriteLine(i);
6 }
7 Console.ReadKey();
8 }
9
10 static IEnumerable<int> GetValues()
11 {
12 yield return 1;
13 yield return 2;
14 yield return 3;
15 }
The yield return statement cannot be placed in the Try-catch, but it can be placed in the try-finally. Yield return has two features: 1. The return type must be IEnumerable, IEnumerator, ienumerator<t>, ienumerable<t>.2. You cannot use the ref and out modifiers. Yield return and yield break cannot be included in anonymous methods and unsafe code. Let's use yield return to simplify the iteration of student in the previous article, re-queue<t> the generic class, as follows
1 class Queue<T> : IEnumerable<T> where T : class
2 {
3 public List<T> objects = new List<T>();
4 int startPoint = 0;
5 public Queue(List<T> list)
6 {
7 objects = list;
8 }
9
10 // Implement the GetEnumerator method from IEnumerable
11 /*
12 people think that this method will only be called once in the iteration, otherwise it will return a new QueueIterator<T> object every time, the location record will be reset to -1
13 */
14 public IEnumerator<T> GetEnumerator()
15 {
16 //return new QueueIterator<T>(this);
17 for (int index = 0; index < objects.Count; index++)
18 {
19
20 yield return objects[(index + startPoint) % objects.Count];
twenty one }
twenty two }
twenty three
24 IEnumerator IEnumerable.GetEnumerator()
25 {
26 throw new NotImplementedException();
27 }
28 }
When the code returns to yield, the appropriate element in objects is returned, and the current state is saved, and the next call starts at the position of the count. In the use of the program, as in the following, and the same as originally.
1 List<Student> list = new List<Student> {
2 new Student("СA"),
3 new Student("СB"),
4 new Student("СC"),
5 new Student("СD"),
6 new Student("СE")
7 };
8 ConsoleDemo.Chapter6.Queue<Student> lq = new Chapter6.Queue<Student>(list);
9
10 foreach (var obj in lq)
11 {
12 obj.SayName();
13 }
You can see that the results are all iterations of the printed name of each element. In terms of the return break, which can be fancy from break, is to jump out, that means to jump out of the iteration, as
1 class Program
2 {
3 static void Main(string[] args)
4 {
5
6 foreach (int i in GetValues())
7 {
8 Console.WriteLine(i);
9 }
10 Console.ReadKey();
11 }
12
13 static IEnumerable<int> GetValues()
14 {
15 yield return 1;
16 yield return 2;
17 yield break;
18 yield return 3;
19 }
20 }
When yield return 2 o'clock, the next statement is yield break, the console will only print 1, 2, because the yield break is used to jump out of the iteration before printing 3.
Below we use an example of the actual point of the read file. Create a new text demo.txt with the following content
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 0
The code is as follows
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 foreach(var line in ReadLines("./demo.txt"))
6 {
7 Console.WriteLine(line);
8 }
9 Console.ReadKey();
10 }
11
12 static IEnumerable<string> ReadLines(string path)
13 {
14 string line;
15 TextReader tr = File.OpenText(path);
16 while ((line = tr.ReadLine()) != null)
17 {
18 yield return line;
19 }
20 }
21 }
Of course, in fact, file in the method is ReadLines method, return is also ienumerable<string>, it seems we are superfluous, but also good, know some of the use of yield, the principle of those who really dare not write, Write oneself also do not understand, hope later can use their own organization language, interpretation of those principles.
Please treatise.
12.c#yield return and yield break and practical applications (Chapter six 6.2-6.4)