IEnumerable and IEnumrator usage

Source: Internet
Author: User

First, use a modified code to demonstrate the usage. It can only be used. [Csharp/** Created by SharpDevelop. * User: Owner * Date: 2012-12-14 * Time: 13:43 ** To change this template use Tools | Options | Coding | Edit Standard Headers. */using System; using System. collections; /// <summary> // IEnumerable and IEnumrator are combined to implement foreach. /// </Summary> namespace TestIEnumeratorAndIEnumratable {// <summary> // individual class: this class stores the information of a single individual and a method // </summary> public class individual {public string Information 1; public string Information 2; public individual (string Information 1, string Information 2) {this. information 1 = Information 1; this. information 2 = Information 2;} public void () {Console. writeLine (this. information 1 + "=" + this. information 2); }}/// <summary> // collection class: inherits from the IEnumerable interface and provides the possibility of foreach. /// </Summary> public class: IEnumerable {// <summary> // The Group is a one-vote individual. // </summary> private individual [] one-vote individual; /// <summary> /// this group is composed of an initial group of individuals for convenience. /// </Summary> /// <param name = "pArray"> </param> public group (individual [] pArray) {one-vote individual = new individual [pArray. length]; for (int I = 0; I <pArray. length; I ++) one-vote individual [I] = pArray [I] ;}/// <summary> // interface implementation function of IEnumerable, get the iterator used by foreach /// </summary> /// <returns> </returns> public IEnumerator GetEnumerator () {return new individual pointer (one-vote individual); }}/// <summary> // individual pointer: The iterator for the individual class inherited from IEnumerator. /// Provides Function Support for the iterator. /// </Summary> public class individual pointer: IEnumerator {// <summary> /// pointer of one-vote individual /// </summary> public individual [] pointer of one-vote individual; /// <summary> /// indicator mark of the iterator's current position /// </summary> int position =-1; /// <summary> /// value assignment of the individual pointer: point to a bunch of unmanaged individuals // </summary> /// <param name = "list"> </param> public individual pointers (individual [] list) {pointer = list ;}//< summary> /// the implementation function of the IEnumerator interface. The iterator moves down. Foreach is used. We do not call it. /// </Summary> /// <returns> </returns> public bool MoveNext () {position ++; return (position <pointer of a single ticket. length) ;}/// <summary> /// interface implementation function of IEnumerator. The iterator is reset. Foreach is used. We do not call it. /// </Summary> public void Reset () {location =-1 ;}/// <summary> // the implementation function of IEnumerator to obtain the current value. Foreach is used. We do not call it. /// </Summary> public object Current {get {try {return pointer [position];} catch (IndexOutOfRangeException) {throw new InvalidOperationException () ;}}} class Program {public static void Main (string [] args) {Individual [] Individual set = new individual [3] {new individual ("1", "One"), new individual ("2", "Two "), new individual ("3", "Three")}; collective list = new collective (individual set); foreach (individual p in list) p. report (); Console. readKey () ;}} the above example clearly demonstrates IEnumerat Or (iterator Interface) and IEnumerable (traversal Interface) usage. It generally represents everything. I want to use the concepts of individuals and groups to better understand this function. As I understand, the independent IEnumerator interface is useless. The IEnumerable interface can work with various classes (ArrayList, Queue, and so on) inherited from IEnumerator provided by the system, without implementation every time.

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.