Basis of iterator in C #

Source: Internet
Author: User

 

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.

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 (Of T), or IEnumerator (Of T ).

The iterator is the basis for delayed execution behavior in the LINQ query.

The yield keyword is used to specify one or more returned values. 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 complex data structures (such as binary trees ).

-- From MSDN

In fact, in my understanding, the iterator is a set of data that can be accessed using foreach.

According to the returned type of the above iterator, we can also use the class to inherit the iterator interface to implement our own iterator.

To implement the class that inherits the IEnumerable interface iterator, you must implement the GetEnumerator () method. For example:

1 public class CityCollection: IEnumerable

2 {

3 string [] _ Items = new string [] {"Heilongjiang", "Jilin", "Liaoning", "Shandong", "Shanxi", "Shaanxi", "Hebei ", "Henan", "Hunan", "Hubei", "Sichuan", "Guangxi", "Yunnan", "Others "};

4 IEnumerator IEnumerable. GetEnumerator ()

5 {

6 for (int I = 0; I <_ Items. Length; I ++)

7 {

8 yield return _ Items [I];

9}

10}

11}

 

In this case, it is a basic iteration class. After we create a CityCollection object, we can use foreach to facilitate it.

1 CityCollection collection = new CityCollection ();

2 foreach (string item in collection)

3 {

4 Console. WriteLine (item );

5}

 

However, IEnumerable is a generic interface. Without a generic interface, it can only be traversed. Instead, it cannot be used to query the set or perform other operations by using the LINQ query method. Therefore, we can modify the above iteration class to add the generic type. The modification is as follows:

1 public class CityCollection: IEnumerable <string>

2 {

3 string [] _ Items = new string [] {"Heilongjiang", "Jilin", "Liaoning", "Shandong", "Shanxi", "Shaanxi", "Hebei ", "Henan", "Hunan", "Hubei", "Sichuan", "Guangxi", "Yunnan", "Others "};

4 IEnumerator <string> IEnumerable <string>. GetEnumerator ()

5 {

6 for (int I = 0; I <_ Items. Length; I ++)

7 {

8 yield return _ Items [I];

9}

10}

11 IEnumerator IEnumerable. GetEnumerator ()

12 {

13 for (int I = 0; I <_ Items. Length; I ++)

14 {

15 yield return _ Items [I];

16}

17}

18}

 

 

Through the above modification, you can use LINQ to query the result set and perform other operations conveniently. I still don't know what this is called, the statement is unclear, so let's say that. If the expert knows this, please advise. Thank you.

 

1 static void Main (string [] args)

2 {

3 CityCollection collection = new CityCollection ();

4 foreach (string item in collection)

5 {

6 Console. WriteLine (item );

7}

8 Console. WriteLine ("query method :");

9 foreach (string item in collection. Where (t => t. Contains ("River ")))

10 {

11 Console. WriteLine (item );

12}

13 System. Console. ReadKey ();

14}

 

The master can ignore it, but I hope the master will give me some advice. What are the professional terms of the where query method in the above Code? Thank you.

 

From luacloud logs

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.