C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet1012/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1008   1009   1010   1011   1012   1013   1014   1015   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

GetEnumerator( )

, five yield statements occur. The important thing to understand 

is that they are executed one at a time, in order, each time another element in the collection 

is obtained. Thus, each time through the 



foreach

 loop in 



Main( )

, one character is returned.



Creating a Named Iterator

Although the preceding examples have shown the easiest way to implement an iterator, 

there is an alternative: the named iterator. In this approach, you create a method, operator, 

or accessor that returns a reference to an 



IEnumerable

 object. Your code will use this object 

to supply the iterator. A named iterator is a method with the following general form:

public IEnumerable 



itr-name

(

param-list

) {

    // ...



    yield return 

obj

;

}



Here, 

itr-name

is the name of the method, 



param-list

specifies zero or more parameters that 

will be passed to the iterator method, and 

obj

 is the next object returned by the iterator. Once 

you have created a named iterator, you can use it anywhere that an iterator is needed. For 

example, you can use the named iterator to control a 



foreach

 loop.


Named iterators are very useful in some circumstances because they allow you to pass 

arguments to the iterator that control what elements are obtained. For example, you might 

pass the iterator the beginning and ending points of a range of elements to iterate. This form 

of iterator can also be overloaded, further adding to its flexibility. The following program 

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1008   1009   1010   1011   1012   1013   1014   1015   ...   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