Implementation of C # enumerator interface IEnumerator

Source: Internet
Author: User

Original text (http://blog.csdn.net/phpxin123/article/details/7897226)

In C #, if a class is going to use a foreach structure to implement an iteration, it must implement the IEnumerable or IEnumerator interface. Where the IEnumerator interface defines the way to implement the enumerator pattern Ienumerator.movenext () and Ienumerator.reset () and member properties Ienumerator.count, and the IEnumerable interface's only Method Ienumerable.getenumerator () is used only to return a IEnumerator object, Used to indirectly implement a IEnumerator interface.

The ienumerator<t> and ienumerable<t> interfaces of generics are similar to normal types, which are generalized forms of the common type enumerator interface. But to implement the Ienumerable<t> interface, two GetEnumerator are implemented, IEnumerator Ienumerable.getenumerator () and ienumerator<t>, respectively GetEnumerator ().

1, the realization of IEnumerator

Look for examples yourself ...

2, the realization of IEnumerable

(1) return a IEnumerator

Look for examples yourself ...

(2) Use yield return (yield break) syntax

Look for examples yourself .....

3, a hybrid implementation of the structure

Implement the IEnumerable interface in one class (such as CLASSNAME1), and implement the IEnumerator interface in another class (such as Classname1enumerator), and ClassName1 () in GetEnumerator Method, a Classname1enumerator object that is instantiated with CLASSNAME1 is returned.

4, the enumerator specific introduction

The enumerator can be used to read the data in the collection, but it cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. The Reset method also returns the enumerator to this location. in this location, calling the current property throws an exception. therefore, before reading the value of current, you must first move the enumerator to the first element in the collection by calling the MoveNext method.

Current returns the same object before calling MoveNext or reset. MoveNext sets current to the next element.

If movenext  crosses the end of the collection, The enumerator is placed behind the last element in the collection, and movenext  returns false. false. If the last call to movenext  returns false, calling  Current  throws an exception. to set current  again as the first element of a collection, you can call reset, Then call MoveNext again.

The enumerator remains in effect as long as the collection remains intact. If changes are made to the collection (such as adding, modifying, or deleting elements), the enumerator becomes invalid (this change is not recoverable), and the next call to MoveNext or reset throws InvalidOperationException. If a collection is modified between MoveNext and current, then current returns the element to which it was set, even if the enumerator is invalidated.

The enumerator does not have exclusive access to the collection, so enumerating the entire collection is inherently not a thread-safe process. even if the collection is synchronized, other threads can still modify the collection, causing the enumerator to throw an exception. to ensure thread safety during enumeration, you can lock the collection during the entire enumeration, or catch exceptions caused by changes made by other threads.

Implementation of C # enumerator interface IEnumerator

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.