New. Net2.0 function: Iterators)

Source: Internet
Author: User
What is an iterator? 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. Example: publicclassYieldTest... {publicstaticIEnumertablePower (intnumber, intexponent)... {intcount

What is an iterator? 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. Example: public class YieldTest... {public static IEnumertablePower (int number, int exponent)... {int count

What is an iterator?

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.

Example:

Public class YieldTest

...{

Public static IEnumertable Power (int number, int exponent)

...{

Int counter = 0;

Int result = 1;

While (counter ++ <exponent)

...{

Result = result * number

Yield return result

}

}

}


1. The iterator is a piece of code that can return an ordered sequence of values of the same type.

2. The iterator can be used as a method, operator, or get accessors code body.

3. The iterator Code uses the yield return statement to return each element at a time.

4. yield break will terminate Iteration

5. You can implement an iterator in the class. Each iterator must have a unique name like any class member and can be called in a foreach statement.

6. The return type of the iterator must be IEnumerable, IEnumberator, and IEnumerbale. Or IEnumberator

Foreach (int I in YieldTest. Power)

{

}

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.