C# 0 The Complete Reference


IEnumerator  and  IEnumerable



Download 4,07 Mb.
Pdf ko'rish
bet1006/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1002   1003   1004   1005   1006   1007   1008   1009   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

IEnumerator

 and 


IEnumerable

, using either their generic or non-generic form. Fortunately, because these 

interfaces are so small, they are easy to implement.

N

OTE

N

OTE

Technically, in order for a class to be used with a 

foreach

 loop, it does not actually have to 

specify

IEnumerator

or

IEnumerable

 as implemented interfaces. It does, however, have to 

provide their methods, which are 

GetEnumerator( )

,

Reset( )

,

MoveNext( )

, and the property 

Current

. However, not specifying these interfaces reduces the usability of the class in a mixed-

language environment.

Here is an example that implements the non-generic versions of 



IEnumerable

 and 


IEnumerator

 so that the contents of the array encapsulated within 



MyClass

 can be 


enumerated:

// Implement IEnumerable and IEnumerator.

using System;

using System.Collections;

class MyClass : IEnumerator, IEnumerable {

  char[] chrs = { 'A', 'B', 'C', 'D' };

  int idx = -1;

  // Implement IEnumerable.

  public IEnumerator GetEnumerator() {

    return this;

  }

  // The following methods implement IEnumerator.



  // Return the current object.

  public object Current {

    get {

www.freepdf-books.com




812

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

      return chrs[idx];

    }

  }


  // Advance to the next object.

  public bool MoveNext() {

    if(idx == chrs.Length-1) {

      Reset(); // reset enumerator at the end

      return false;

    }


    idx++;

    return true;

  }

  // Reset the enumerator to the start.



  public void Reset() { idx = -1; }

}

class EnumeratorImplDemo {



  static void Main() {

    MyClass mc = new MyClass();

    // Display the contents of mc.

    foreach(char ch in mc)

      Console.Write(ch + " ");

    Console.WriteLine();

    // Display the contents of mc, again.

    foreach(char ch in mc)

      Console.Write(ch + " ");

    Console.WriteLine();

  }

}

Here is the output:



A B C D

A B C D


In the program, first examine 

MyClass

. It encapsulates a small 



char

 array that contains 

the characters A through D. An index into this array is stored in 

idx

, which is initialized to –1. 



MyClass

 then implements both 



IEnumerator

 and 


IEnumerable

.

GetEnumerator( )

 returns a 

reference to the enumerator, which in this case is the current object. The 



Current

 property 

returns the next character in the array, which is the object at 

idx

. The 


MoveNext( )

 method 


advances

idx

 to the next location. It returns false if the end of the collection has been reached 

and true otherwise. 

Reset( )

 sets 


idx

 to –1. Recall that an enumerator is undefined until after 

the first call to 

MoveNext( )

. Thus, in a 



foreach

 loop, 


MoveNext( )

 is automatically called 

before 

Current

. This is why 



idx

 must initially be –1; it is advanced to zero when the 



foreach

loop begins. A generic implementation would work in a similar fashion.

Inside

Main( )

, an object of type 



MyClass

 called 


mc

 is created and the contents of the 

object are twice displayed by use of a 

foreach

 loop.


www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1002   1003   1004   1005   1006   1007   1008   1009   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish