Basic _ C # iterator

Source: Internet
Author: User
Document directory
  • Iterator Overview
C # iterator
The iterator is a new feature in C #2.0. An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or structure without implementing the entire ienumerable interface. You only need to provide an iterator to traverse the data structure in the class. When the compiler detects the iterator, it automatically generatesCurrent,MoveNextAndDisposeMethod.
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, or get accessors code body.

  • The iterator Code uses the yield return statement to return each element in sequence. Yield break terminates iteration. For more information, see yield.

  • Multiple iterators can be implemented in the class. Each iterator must have a unique name like any class member and can be called by the client code in the foreach statement, as shown below:foreach(int x in SampleClass.Iterator2){}

  • The return type of the iterator must be ienumerable, ienumerator, ienumerable, or ienumerator.

The yield keyword is used to specify the returned value. When the yield Return Statement is reached, the current position is saved. The next time you call the iterator, it will start execution again from this position.

The iterator is particularly useful for collection classes. It provides a simple method to iterate infrequently used data structures (such as binary trees ).

Simple Example

In this example,DaysOfTheWeekClass is a simple set class that stores the day of a week as a string. Each iteration of the foreach loop returns the Next string in the set.

public class DaysOfTheWeek : System.Collections.IEnumerable{    string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };    public System.Collections.IEnumerator GetEnumerator()    {        for (int i = 0; i < m_Days.Length; i++)        {            yield return m_Days[i];        }    }}class TestDaysOfTheWeek{    static void Main()    {        // Create an instance of the collection class        DaysOfTheWeek week = new DaysOfTheWeek();        // Iterate with foreach        foreach (string day in week)        {            System.Console.Write(day + " ");        }    }}

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.