C# 0 The Complete Reference


Accessing a Collection via an Enumerator



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

Accessing a Collection via an Enumerator

Often you will want to cycle through the elements in a collection. For example, you might 

want to display each element. One way to do this is to use a 

foreach

 loop, as the preceding 

examples have done. Another way is to use an enumerator. An 

enumerator

 is an object that 

implements either the non-generic 

IEnumerator

 or the generic 



IEnumerator

 interface.



IEnumerator

 defines one property called 



Current

. The non-generic version is shown here:

object Current { get; }

For


IEnumerator

,

Current

 is declared like this:

T Current { get; }

In both cases, 

Current

 obtains the current element being enumerated. Since 



Current

 is a 


read-only property, an enumerator can only be used to retrieve, but not modify, the objects 

in a collection.



IEnumerator

 defines two methods. The first is 



MoveNext( )

:

bool MoveNext( )



Each call to 

MoveNext( )

 moves the current position of the enumerator to the next element 

in the collection. It returns true if the next element is available, or false if the 

end

 of the 


collection has been reached. Prior to the first call to 

MoveNext( )

, the value of 



Current

 is 


undefined. (Conceptually, prior to the first call to 

MoveNext( )

, the enumerator refers to the 

nonexistent element that is just before the first element. Thus, you must call 

MoveNext( )

 to 


move to the first element.)

You can reset the enumerator to the start of the collection by calling 



Reset( )

, shown here:

void Reset( )

After calling 



Reset( )

, enumeration will again begin at the start of the collection. Thus, you 

must call 

MoveNext( )

 before obtaining the first element.

In

 IEnumerator

, the methods 



MoveNext( )

 and 


Reset( )

 work in the same way.

Two other points: First, you cannot use an enumerator to change the collection that it is 

enumerating. Thus, enumerators are read-only relative to the collection. Second, any change 

to the collection under enumeration invalidates the enumerator.

Using an Enumerator

Before you can access a collection through an enumerator, you must obtain one. Each of the 

collection classes provides a 

GetEnumerator( )

 method that returns an enumerator to the 

start of the collection. Using this enumerator, you can access each element in the collection, 

one element at a time. In general, to use an enumerator to cycle through the contents of a 

collection, follow these steps:

www.freepdf-books.com




Download 4,07 Mb.

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