How to build enumerable types in C # (IEnumerable and IEnumerator)

Source: Internet
Author: User
Tags foreach reset

To begin understanding the implementation of existing interfaces, let's take a look at the role of IEnumerable and IEnumerator, and think that C # supports the keyword foreach, allowing us to traverse the contents of any array type:

Traversal of an array of items
int[] myarray={10,20,30}
foreach (int i in myarray)
{...}

Although it seems that only an array type can use this structure, any type that supports the GetEnumerator () method can be performed by using the foreach structure, for example, we create a new project.

First, we create a class

public class car
    {public
        int speed{get;set;}
        public string PetName {get; set;}
    
        public car (int s, string p)
        {this
            . Speed = s;
            This.petname = P;
        }
    }

Next we'll build a class to save a set of car types in System.Array

public class Garage
    {
        private car[] Cararray = new Car[4];
        Public Garage ()
        {
            cararray[0] = new Car ("Rusty");
            Cararray[0] = new Car ("Tom");
            Cararray[0] = new Car ("Kimm");
            Cararray[0] = new Car ("FRED");
        }
    

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

Ideally, it is convenient to use the C#foreach structure to iterate over each subkey in the garage object.

static void Main (string[] args)
        {
            Garage carlot = new Garage ();
            foreach (car C in Carlot)
            { 
                    
            }
        }

But the compiler informs us that the garage class does not implement a public definition named GetEnumerator (). Objects support this behavior by stating that they must be able to provide the caller with the subkeys they contain.

This interface informs the caller that the subkeys of the object can enumerate public
interface IEnumerable
{
    IEnumerator GetEnumerator ();
}

As you can see, the GetEnumerator () method returns a reference to the IEnumerator of another interface that provides the infrastructure that the caller can use to move the internal objects that the IEnumerable compatible container contains.

This interface allows callers to obtain a subkey of the container public
    interface Ienumeratoe
    {
        bool MoveNext ();
        Object current {get;}
        void Reset ();
    }

If you want to modify the garage class to support these interfaces, we can implement these methods manually, but this is cumbersome. Although it is no problem to develop your own GetEnumerator () MoveNext () Reset (), there is a simpler way. Because the System.Array type and many other types have been implemented IEnumerable and IEnumerator interfaces, we can simply delegate requests to System.Array, as follows

public class garage:ienumerable
    {
        private car[] Cararray = new Car[4];
        Public Garage ()
        {
            cararray[0] = new Car ("Rusty");
            Cararray[0] = new Car ("Tom");
            Cararray[0] = new Car ("Kimm");
            Cararray[0] = new Car ("FRED");
    
        Public IEnumerator GetEnumerator ()
        {
            //system. Array has implemented the IEnumerator return
            Cararray.getenumerator ();
        }
    

Once modified, we can use foreach to iterate through it. In addition, GetEnumerator () is defined as public and the object user can interact with the IEnumerator type.

Manual and IEnumerator collaboration
IEnumerator I=carlot.getenumerator ();
I.movenext ();
Car mycar= (car) i.cuurent;

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.