c#2.0 iterators

Source: Internet
Author: User

iterators

The iterator pattern is an example of a pattern, and we access all the elements in the data series without worrying about what the sequence is. From the data pipeline, the data passes through a series of different transformations or filters and comes out from the other end of the pipeline.

like arrays, collections, etc. already built-in iterators, we can directly do the foreach, and our custom type, naturally cannot do this operation, for the custom type implements the iterator pattern through IEnumerable and the IEnumerator interface (or generic interface) is implemented. In c#1.0 Our custom type implementation iterator is very cumbersome

Custom Iterators

want to Iterationsample A foreach, first inheriting the IEnumerable interface, there will be a GetEnumerator method to implement, at this time to create another class implementation IEnumerator Interface, implement the MoveNext method and The current property, we need to handle the location

At this point, you can use the "syntax Sugar" to traverse, why is the syntax of sugar, because this is only a Microsoft packaging processing, after compiling the IL will still see the call GetEnumerator, MoveNext and Current

here in C#2.0 is easy to change, then using yield return can replace the entire Iterationsampleiterator class.

Yield return tells the compiler that this is not a common method, but rather a method of implementing an iterator block.

At this point there may be doubts, all yield return is not the end of the method, not so , yield return does not mean that the method is executed , but will be here to temporarily exit. Yield return internally generates a state machine for us to maintain the MoveNext method with the current property

then maybe you want to see What kind of code does IL generate,

you can see that there is a State (status)current (index) index, which is used to record the position in the block and the value of the local variable, which actually looks and Iterationsampleiterator Similar, just to help us do this thing

May have doubts about the temporary exit

Here is an example to help understand that the yield return before and after the corresponding input,for Loop three times, at the end of the method last printing method

The following call

you can see the following input, you can see the first execution MoveNext method will run the Createenumerable method, that is, foreach is lazy lazy, you can see the yield return method is temporarily exited, only output the before yield return , the next time the MoveNext will continue to execute output yield return from the last position , the end of the loop will not be output until the end of the method

In addition to yield return, there is yield break, which is the real direct exit quite with the normal method of return

With a custom iterator, you can simplify the traversal of the type, and sometimes we need to read the first line of the file, and that code is often used. Implementing an iterator model can simplify your code even more.

c#2.0 iterators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.