C # iterator implementation

Source: Internet
Author: User

To complete an iterator to a class with a class that has already implemented the iteration

In general, if you want to enumerate the class contents contained in a class by using foreach, you must implement the enumeration interface IEnumerable and then re-write the GetEnumerator method. It is simpler to create the content to be loaded into an array or class that already implements the enumeration, and then return the array or the GetEnumerator method in this class. As shown below

    classProgram { Public Static voidMain (string[] args) {Console.WriteLine ("Hello world!"); //todo:implement functionality hereClass classs=NewClass (); foreach(intIteminchclasss) {Console.WriteLine (""+item); } console.write ("Press any key to continue ..."); Console.readkey (true); }    }     Public classclass:ienumerable {int[] Myselfdata=New int[]{1,2,3};  PublicClass () {} PublicIEnumerator GetEnumerator () {returnMyselfdata.        GetEnumerator (); }    }

Second, the use of the yield keyword to implement the iterator, you can also implement a self-named iterator.

The yield keyword is used to return the specified value to the caller's foreach structure, the current position is recorded when yield return returns, and the yield break exits the iteration the next time it is returned from the current position.

classProgram { Public Static voidMain (string[] args) {Console.WriteLine ("Hello world!"); //todo:implement functionality hereClass2 classs=NewClass2 (); //GetEnumerator Default iterator            foreach(intIteminchclasss) {Console.WriteLine ("Default iterator GetEnumerator"+item); }            //getmydata iterators, Custom iterators, where the error is temporarily not found//foreach (int item in CLASSS. Getmydata (true))//            {//Console.WriteLine ("Self-named iterator getmydata" +item);//            }            //custom command iterators can also be implemented directly using interfacesIEnumerator mydata= classs. Getmydata (false);  while(MyData. MoveNext ()) {Console.WriteLine ("self-named iterator Getmydata"+MyData.            Current); } console.write ("Press any key to continue ..."); Console.readkey (true); }    }         Public classclass2:ienumerable {int[] Myselfdata=New int[]{1,2,3};  PublicClass2 () {} PublicIEnumerator GetEnumerator () {yield returnmyselfdata[0]; yield returnmyselfdata[1]; yield returnmyselfdata[2]; //or//foreach (int item in myselfdata)//            {//yield return item;//            }        }         PublicIEnumerator Getmydata (BOOLdes) {            if(des) {yield returnmyselfdata[0]; yield returnmyselfdata[1]; yield returnmyselfdata[2]; }            Else            {            yield returnmyselfdata[2]; yield returnmyselfdata[1]; yield returnmyselfdata[0]; }        }    }    

C # iterator implementation

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.