C# 0 The Complete Reference


PART II C h a p t e r   2 4



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


PART II

C h a p t e r   2 4 :  

C o l l e c t i o n s ,   E n u m e r a t o r s ,   a n d   I t e r a t o r s    

809


 

1.  Obtain an enumerator to the start of the collection by calling the collection’s 



GetEnumerator( )

 method.


 

2.  Set up a loop that makes a call to 



MoveNext( )

. Have the loop iterate as long as 



MoveNext( )

 returns true.

 

3.  Within the loop, obtain each element through 



Current

.

Here is an example that implements these steps. It uses an 



ArrayList

, but the general 

principles apply to any type of collection, including the generic collections.

// Demonstrate an enumerator.

using System;

using System.Collections;

class EnumeratorDemo {

  static void Main() {

    ArrayList list = new ArrayList(1);

    for(int i=0; i < 10; i++)

      list.Add(i);

    // Use enumerator to access list.

    IEnumerator etr = list.GetEnumerator();

    while(etr.MoveNext())

      Console.Write(etr.Current + “ “);

    Console.WriteLine();

    // Re–enumerate the list.

    etr.Reset();

    while(etr.MoveNext())

      Console.Write(etr.Current + “ “);

    Console.WriteLine();

  }


}

The output is shown here:

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

In general, when you need to cycle through a collection, a 

foreach

 loop is more 

convenient to use than an enumerator. However, an enumerator gives you a little extra 

control by allowing you to reset the enumerator at will.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   999   1000   1001   1002   1003   1004   1005   1006   ...   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