C# 0 The Complete Reference


Using the IDictionaryEnumerator



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

Using the IDictionaryEnumerator

When using a non-generic 



IDictionary

, such as 



Hashtable

, you will use an 



IDictionaryEnumerator

 instead of an 



IEnumerator

 when cycling through the collection. 

The

IDictionaryEnumerator

 inherits 



IEnumerator

 and adds three properties. The first is

DictionaryEntry Entry { get; }

www.freepdf-books.com




810

 

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

Entry

 obtains the next key/value pair from the enumerator in the form of a 



DictionaryEntry

structure. Recall that 



DictionaryEntry

 defines two properties, called 



Key

 and 


Value

, which 


can be used to access the key or value contained within the entry. The other two properties 

defined by 



IDictionaryEnumerator

 are shown here:

object Key { get; }

object Value { get; }

These allow you to access the key or value directly.

An

IDictionaryEnumerator

 is used just like a regular enumerator, except that you will 

obtain the current value through the 



Entry

,

Key

, or 

Value

 properties rather than 



Current

.

Thus, after obtaining an 



IDictionaryEnumerator

, you must call 



MoveNext( )

 to obtain the 

first element. Continue to call 

MoveNext( )

 to obtain the rest of the elements in the collection. 



MoveNext( )

 returns false when there are no more elements.

Here is an example that enumerates the elements in a 

Hashtable

 through an 



IDictionaryEnumerator

:

// Demonstrate IDictionaryEnumerator.



using System;

using System.Collections;

class IDicEnumDemo {

  static void Main() {

    // Create a hash table.

    Hashtable ht = new Hashtable();

    // Add elements to the table.

    ht.Add("Tom", "555–3456");

    ht.Add("Mary", "555–9876");

    ht.Add("Todd", "555–3452");

    ht.Add("Ken", "555–7756");

    // Demonstrate enumerator.

    IDictionaryEnumerator etr = ht.GetEnumerator();

    Console.WriteLine("Display info using Entry.");

    while(etr.MoveNext())

     Console.WriteLine(etr.Entry.Key + ": " +

                       etr.Entry.Value);

    Console.WriteLine();

    Console.WriteLine("Display info using Key and Value directly.");

    etr.Reset();

    while(etr.MoveNext())

     Console.WriteLine(etr.Key + ": " +

                       etr.Value);

  }


}

The output is shown here:

www.freepdf-books.com



Download 4,07 Mb.

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