C # foreach implementation principle,
This article mainly records my experiences in learning the foreach traversal principle in C.
Traversing elements in a set is a common operation involved in all encoding. Therefore, most programming languages write this process into the syntax, such as foreach in C. The following traversal code is often displayed:
var lstStr = new List<string> { "a", "b" }; foreach (var str in lstStr) { Console.WriteLine(str); }
The actual code execution process is as follows:
var lstStr = new List<string> {"a", "b"}; IEnumerator<string> enumeratorLst = lstStr.GetEnumerator(); while (enumeratorLst.MoveNext()) { Console.WriteLine(enumeratorLst.Current); }
The GetEnumerator () method and IEnumerator <string> type are found, which involves the concepts of enumeration types and enumerators.
For ease of understanding, the following is a non-generic example:
// Abstract: // public enumerator, which supports simple iteration on non-generic sets. Public interface IEnumerable {// Abstract: // returns an enumerator for the cyclic access set. //// Return result: // The System. Collections. IEnumerator object that can be used to access the set cyclically. IEnumerator GetEnumerator ();}
The class that implements this interface is called the enumerable type and can be traversed using foreach.
The Return Value of the GetEnumerator () method is an enumerator, which can be understood as a cursor.
// Abstract: // supports simple iteration of non-generic sets. Public interface IEnumerator {// Abstract: // gets the current element in the set. //// Return result: // The current element in the set. //// Exception: // System. InvalidOperationException: // The number of enumerations is located before or after the first element of the set. Object Current {get;} // Summary: // pushes the number of enumerations to the next element of the set. //// Return result: // true if the enumerated number is successfully pushed to the next element. false if the enumerated number is beyond the end of the set. //// Exception: // System. InvalidOperationException: // The set is modified after the enumeration number is created. Bool MoveNext (); // Abstract: // set the number of enumerations to its initial position before the first element in the set. //// Exception: // System. InvalidOperationException: // The set is modified after the enumeration number is created. Void Reset ();}
The following is an example of customizing an iterator (https://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx ):
using System;using System.Collections;// Simple business object.public class Person{ public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName;}// Collection of Person objects. This class// implements IEnumerable so that it can be used// with ForEach syntax.public class People : IEnumerable{ private Person[] _people; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } }// Implementation for the GetEnumerator method. IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator(); } public PeopleEnum GetEnumerator() { return new PeopleEnum(_people); }}// When you implement IEnumerable, you must also implement IEnumerator.public class PeopleEnum : IEnumerator{ public Person[] _people; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public PeopleEnum(Person[] list) { _people = list; } public bool MoveNext() { position++; return (position < _people.Length); } public void Reset() { position = -1; } object IEnumerator.Current { get { return Current; } } public Person Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } }}class App{ static void Main() { 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); }}/* This code produces output similar to the following: * * John Smith * Jim Johnson * Sue Rabon * */
With the keyword yield, we can create an enumerator in this way:
using System;using System.Collections;// Simple business object.public class Person{ public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName;}// Collection of Person objects. This class// implements IEnumerable so that it can be used// with ForEach syntax.public class People : IEnumerable{ private Person[] _people; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } // Implementation for the GetEnumerator method. IEnumerator IEnumerable.GetEnumerator() { for (int i = 0; i < _people.Length; i++) { yield return _people[i]; } }}class App{ static void Main() { 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); }}