IEnumerable this interface is said on MSDN, it is a public enumerator that supports simple iterations on non-generic collections. In other words, the traversal of all arrays comes from IEnumerable, so we can use this feature to define a common way to traverse strings.
The following code is posted first.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.collections;namespace mycs{class Program {static void Main (string[] args) {charlist Mycharlis t = new charlist ("Hello World"); foreach (var c in mycharlist) {Console.Write (c); } console.readline (); }} class Charlist:ienumerable {public string targetstr {get; set;} Public charlist (String str) {this. Targetstr = str; } public IEnumerator GetEnumerator () {//c# 1.0 return to new Chariterator (this. TARGETSTR); C # 2.0/* for (int index = this.) Targetstr.length; Index > 0;index--) {yield return this. TARGETSTR[INDEX-1]; } */}} class Chariterator:ienumerator {public string targetstr {get; set;} public int position {get; set;} Public Chariterator (String targetstr) {this. Targetstr = Targetstr; This.position =This. Targetstr.length; Public object Current {get {if (this.position==-1| | This.position==this. Targetstr.length) {throw new InvalidOperationException (); } return this. Targetstr[this.position]; }} public bool MoveNext () {if (this.position!=-1) {this.position--; } return this.position >-1; The public void, Reset () {this.position = this. Targetstr.length; } }}
In the above example C # 1.0, Chariterator is the implementation of the iterator, the position field stores the current iteration position, through the current property can get the element of the present iteration position, the MoveNext method is used to update the iteration position, And see if the next iteration position is valid.
When we debug the following statement through VS, one step at a time:
The code is as follows:
foreach (var c in charList)
The code first executes to the charlist of the foreach statement to get an instance of the iterator Chariterator, then the code executes to the MoveNext method in which the iterator is called, and the last variable C gets the value of the iterator current property; Starts a new round of loops, calls the MoveNext method, and gets the value of the current property.
The code for the iterator in C # 1.0 sees that implementing an iterator will implement the IEnumerator interface and then implement the MoveNext, reset method, and current property in the IEnumerator interface.
The yield statement can be used directly in C # 2.0 to simplify the implementation of iterators.
As commented out in the public IEnumerator GetEnumerator () method above.
As you can see from the code above, we can replace the entire Chariterator class by using the yield return statement.
The yield return statement tells the compiler to implement an iterator block. If the return type of the GetEnumerator method is a non-generic interface, then the build type of the iterator block (yield type) is object, otherwise the type parameter of the generic interface.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Introduction to IEnumerable and simple implementation examples in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23109.html
Related Content C #. NET based on regular expression to crawl Baidu hundreds of articles List of methods Example C # Two simple code example to connect to MySQL C # MVC payment Tutorial series of code for the scan code example c#êμ????? Aêμó?μ? SQLhelper
C # Methods for controlling thread queues through the Semaphore Class C # Implementation of JSON serialization and deserialization code instances C # Timer timer Usages Example C # implements the method of automatically adding copyright annotation information to class and function code
Introduction to IEnumerable and simple implementation examples in C #