You can use foreach to traverse the accessed object. You need to implement the ____ interface or declare the ____ method type.

Source: Internet
Author: User

I. Answer

You can use foreach to traverse the accessed objects.IenumerableInterface or declarationGetenumeratorMethod Type

Note: you do not have to implement the ienumerable interface, but you must implement the getenumrator method.

Ii.. NET 1.0 implementation

Code
Public class mylist <t>: ienumerable
{
Public int count {get {return items = NULL? 0: items. Length ;}}

Public T [] items {Get; set ;}

Public t this [int Index]
{
Get {return items [Index];}
}

// Returns the number of enumerations of a set of circular access.
Public ienumerator getenumerator ()
{
Return new myenumerator <t> () {list = this };
}
}

Public class myenumerator <t>: ienumerator
{
Private int Index =-1;
Public mylist <t> List {Get; set ;}

// Set the enumeration number to its initial position before the first element in the set.
Public void reset ()
{
Index =-1;
}

// Push the number of enumerations to the next element of the set.
Public bool movenext ()
{
Index ++;
Return (index <list. Count );
}

// Obtain the current element in the set.
Public object current {get {return list [Index];}
}

// Client call. Note: no generic type exists in 1.0.
Mylist <int> List = new mylist <int> () {items = new int [] {1, 2, 3, 4 }};
Foreach (INT item in List)
{
MessageBox. Show (item. tostring ());
}

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.