C # ienumerable Interface

Source: Internet
Author: User

Ienumerable Interface
Public enumeration number, which supports simple iteration on a non-generic set.
Namespace: system. Collections

Public class person {Public String firstname; Public String lastname; Public Person (string fname, string lname) {This. firstname = fname; this. lastname = lname;} public class people: ienumerable {private person [] _ person; Public people (person [] parray) {_ person = new person [parray. length]; for (INT I = 0; I <parray. length; I ++) _ person [I] = parray [I];} public peopleenum getenumerator () // return peopleenum current instance {return New peopleenum (_ person );} ienumerator ienumerable. getenumerator () {return (ienumerator) getenumerator () ;}} public class peopleenum: ienumerator {int position =-1; public person [] _ person; public peopleenum (person [] list) {_ person = List;} public person current {get {try {return _ person [position];} catch (indexoutofrangeexception) {Throw new invalidoperationexception () ;}} object ienumerator. current {get {Return Current;} public bool movenext () {position ++; Return (position <_ person. length) ;}public void reset () {position =-1 ;}}

Test:

static void Main(string[] args)        {            Person[] peopleArray = new Person[3]             {                 new Person("John", "Smith"),                new Person("Jim", "Johnson"),                new Person("Sue", "Rabon")            };            People peopleList = new People(peopleArray);            foreach (Person p in peopleList)                Console.WriteLine((p.firstName + " " + p.lastName));            Console.ReadKey();        }

The effect is as follows:

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.