Notes for implementing IEnumerable and IEnumerator to enable enumeration for Types

Source: Internet
Author: User

Class that implements the IEnumerable interface. It supports the foreach loop to traverse the collection elements of objects.

IEnumerable:
IEnumerator GetEnumerator () returns the number of enumerations that can access the set cyclically.
IEnumerator:
Object Current gets the Current element in the set. Bool MoveNext () pushes the number of enumerations to the next element of the set.
If the enumerated number is successfully pushed to the next elementTrueIf the number of enumerations exceeds the end of the setFalse. Void Reset () sets the number of enumerations to its initial position before the first element in the set.

 

Background

In practical applications, collections and enumeration of these sets are very common. Therefore, the interfaces on which the. NET integration depends are designed to be public. To enumerate objectsMust inheritIEnumerable interface.
Public interface IEnumerable
{
Public IEnumerator GetEnumerator ();
}

It has only one member: the GetEnumerator method. This method returns an actual enumerator object. To define an enumeratorRequiredIEnumrator interface:
Public interface IEnumerator
{
Public Boolean MoveNext ();
Public Object Current {get ;}
Public void Reset ();
}

Here I don't want to repeat the description on MSDN. I just raised a question, that is, if we want a type to support the enumeration feature, then we must define an additional auxiliary type to implement all the methods of IEnumerator. This auxiliary type is usually defined as a Nested class declared inside the main class. In this way, we find that when the app calls the GetEnumerator () method, we need to construct an auxiliary instance as the return value. In this case, we have two methods:

  1. Assign a value to the secondary instance (enumerator) as a duplicate of the current static snapshot of the primary data during the initialization of the secondary type)
  2. Transfers the reference of the primary data type to the secondary data type to ensure real-time update of data accessed by the app.

This does provide some flexibility for users. When the app wants to enumerate a dataset of an instance, there are two methods:

  1. Methods exposed Using Interfaces
  2. Use the foreach statement: foreach (type-identifier inExpression) {Embedded-statement}

For foreach, I would like to mention two conditions that must be met to use it:

  1. ExpressionThe GetEnumerator () method must be implemented.
  2. The put back instance of GetEnumerator () must implement the MoveNext () and Current public methods.
Question 1: Why do I need two interfaces instead of putting the Current, MoveNext, and Reset members into the IEnumerable interface?

Answer: This indirect method is used to improve flexibility. You can use IEnumerator to determine how to expose internal data to customers. Of course, you can implement these two interfaces simultaneously in a type: class MyArrayData: IEnumerable, IEnumerator {...}; this saves some CPU and Memory overhead caused by creating secondary types. We recommend that you use this method in the following situations:

  1. If the data is not stored in an existing collection, the enumerator of the collection can be directly returned.
  2. When your custom Enumerator needs to perform operations other than moving the cursor and returning data
Question 2: if the data in the set is of the value type, what performance considerations are there?

Answer: The value type requires box when it is passed into a collection container such as ArrayList, and unbox when it is passed out. After a lot of verification, this operation is very time-consuming. Therefore, when implementing standard enumeration methods, we can expose some custom GetEnumerator, MoveNext, and Current methods (in fact, the standard interface is only a wrapper method that calls a custom interface ). In this way, you can avoid unnecessary packing and unpacking operations in your own methods (using custom methods instead of using foreach statements ), it can also be called by the conventional foreach syntax (of course, in this case, the box and unbox cannot be avoided ).

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.