C# 0 The Complete Reference



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

GetEnumerator( )

 method, which is shown again here for your convenience.

www.freepdf-books.com



814

 

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

// This iterator returns the characters

// in the chrs array.

public IEnumerator GetEnumerator() {

  foreach(char ch in chrs)

    yield return ch;

}

This is the iterator for 



MyClass

. Notice that it implicitly implements the 



GetEnumerator( )

method defined by 



IEnumerable

. Now, look at the body of the method. It contains a 



foreach

 loop that returns the elements in 



chrs

. It does this through the use of a 



yield return

statement. The 



yield return

 statement returns the next object in the collection, which in this 

case is the next character in 

chrs

. This feature enables 



mc

 (a 


MyClass

 object) to be used 

within the 

foreach

 loop inside 



Main( )

.

The term 



yield

 is a 


contextual keyword

 in the C# language. This means that it only has 

special meaning inside an iterator block. Outside of an iterator, 

yield

 can be used like any 

other identifier.

One important point to understand is that an iterator does not need to backed by an 

array or other type of collection. It simply must return the next element in a group of 

elements. This means the elements can be dynamically constructed using an algorithm. For 

example, here is a version of the previous program that returns all uppercase letters in the 

alphabet. Instead of using an array, it generates the letters using a 



for

 loop.


// Iterated values can be dynamically constructed.

using System;

using System.Collections;

class MyClass {

  char ch = 'A';

  // This iterator returns the letters of the alphabet.

  public IEnumerator GetEnumerator() {

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

      yield return (char) (ch + i);

  }


}

class ItrDemo2 {

  static void Main() {

    MyClass mc = new MyClass();

    foreach(char ch in mc)

      Console.Write(ch + " ");

    Console.WriteLine();

  }


}

The output is shown here:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1005   1006   1007   1008   1009   1010   1011   1012   ...   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