The concept and two implementation methods of the iterator in C #

Source: Internet
Author: User

1. First, let's take a look at the definition of the IEnumerable interface:
Namespace System. Collections
{
// Summary:
// Exposes the enumerator, which supports a simple iteration over a non-generic
// Collection.
[ComVisible (true)]
[Guid ("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
Public interface IEnumerable
{
// Summary:
// Returns an enumerator that iterates through a collection.
//
// Returns:
// An System. Collections. IEnumerator object that can be used to iterate through
// The collection.
[DispId (-4)]
IEnumerator GetEnumerator ();
}
}

This interface is the top priority, and the iterator can implement this interface for iteration.

2. You must also know the yield keyword of C #.
Yield is used in the iterator block to provide values to enumeration objects or send an iteration end signal. It has two forms:
Yield return <expression>;
Yield break;

Note:
Expression must be implicitly converted to the yield type of the iterator.
Yield statements can only appear in the iteration module, but not in insecure modules or anonymous methods. When used together with expressions, the yield return statement cannot appear in catch blocks or try blocks containing one or more catch clauses.

3. introduce the concept of iterator
In fact, the so-called iteration refers to a loop, and the iterator refers to a way to implement this loop.
The iterator is a function added by C #2.0. It can be a method, get accessor or operator. In the end, it enables you to support foreach iteration in the class or structure. You do not need to implement the entire IEnumerable interface in implementation. You only need to provide an iterator. When the compiler detects the iterator time, the Current, MoveNext, and Dispose methods of the IEnumerable or IEnumerable <T> interface are automatically generated.
An iterator is a piece of code that can return an ordered sequence of values of the same type. It uses the yield return statement to return each element at a time and uses yield break to terminate the iteration, the return type must be IEnumerable, IEnumerator, IEnumerable <T>, or IEnumerator <T>.

3. The most common method to create an iterator is to implement the GetEnumerator method in the IEnumerable interface. The IEnumerable interface is as follows:
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace ctochina.net
{
Class Program
{
Static void Main (string [] args)
{
MyIterator myIterator = new MyIterator ();

Foreach (int I in myIterator)
{
Console. WriteLine (I );
}
}
}

Public class MyIterator
{
Public IEnumerator GetEnumerator ()
{
For (int I = 1; I <8; I ++)
{
Yield return I;
}
}
}

}

Output:
1 2 3 4 5 6 7

Note System. Collections; namespace reference, because IEnumerator is in
The GetEnumerator method in the IEnumerable interface is implemented, so only one GetEnumerator iteration can be performed in a class.

4. Another method to implement IterationThe code is followed by the following example:

Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace ctochina.net
{
Class Program
{
Static void Main (string [] args)
{
MyIterator myIterator = new MyIterator ();

// Iteration 1

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.