C# 0 The Complete Reference


PART II C h a p t e r   2 4



Download 4,07 Mb.
Pdf ko'rish
bet981/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   977   978   979   980   981   982   983   984   ...   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    

791


When enumerated, 

SortedDictionary

 returns key/value pairs in the form of a 



KeyValuePair

 structure. Recall that this structure defines the following two fields:

public TK Key;

public TV Value;

These fields hold the key or value associated with an entry. Most of the time you won’t need 

to use 


KeyValuePair

 directly because 



SortedDictionary

 allows you to 

work the keys and values individually. However, when enumerating a 

SortedDictionary

, such as in a 



foreach

 loop, the objects being enumerated are 



KeyValuePair

s.

In a 



SortedDictionary

, all keys must be unique, and a key must not change 

while it is in use as a key. Values need not be unique.

Here is an example that demonstrates 



SortedDictionary

. It reworks the 



Dictionary

 example shown in the preceding section. In this version, the database 

of employees and salaries is sorted based on name (which is the key).

// Demonstrate the generic SortedDictionary class.

using System;

using System.Collections.Generic;

class GenSortedDictionaryDemo {

  static void Main() {

    // Create a Dictionary that holds employee

    // names and their corresponding salary.

    SortedDictionary dict =

      new SortedDictionary();

    // Add elements to the collection.

    dict.Add("Butler, John", 73000);

    dict.Add("Swartz, Sarah", 59000);

    dict.Add("Pyke, Thomas", 45000);

    dict.Add("Frank, Ed", 99000);

    // Get a collection of the keys (names).

    ICollection c = dict.Keys;

    // Use the keys to obtain the values (salaries).

    foreach(string str in c)

      Console.WriteLine("{0}, Salary: {1:C}", str, dict[str]);

  }

}

The output is shown here:



Butler, John, Salary: $73,000.00

Frank, Ed, Salary: $99,000.00

Pyke, Thomas, Salary: $45,000.00

Swartz, Sarah, Salary: $59,000.00

As you can see, the list is now sorted based on the key, which is the employee’s name.

www.freepdf-books.com




792

 

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


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   977   978   979   980   981   982   983   984   ...   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