C# 0 The Complete Reference


PART II C h a p t e r   2 4



Download 4,07 Mb.
Pdf ko'rish
bet1001/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   997   998   999   1000   1001   1002   1003   1004   ...   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    

807


This method compares 

obj1

 with 


obj2

 and returns greater than zero if 



obj1

 is greater than 



obj2,

 zero if the two objects are the same, and less than zero if 



obj1

 is less that 



obj2

.

Here is a generic version of the preceding program that uses 



IComparer

. It 


produces the same output as the previous versions of the program.

// Use IComparer.

using System;

using System.Collections.Generic;

// Create an IComparer for Inventory objects.

class CompInv : IComparer where T : Inventory {

  // Implement the IComparer interface.

  public int Compare(T obj1, T obj2) {

    return obj1.name.CompareTo(obj2.name);

  }


}

class Inventory {

  public string name;

  double cost;

  int onhand;

  public Inventory(string n, double c, int h) {

    name = n;

    cost = c;

    onhand = h;

  }


  public override string ToString() {

    return

      String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}",

                    name, cost, onhand);

  }

}

class GenericIComparerDemo {



  static void Main() {

    CompInv comp = new CompInv();

    List inv = new List();

    // Add elements to the list.

    inv.Add(new Inventory("Pliers", 5.95, 3));

    inv.Add(new Inventory("Wrenches", 8.29, 2));

    inv.Add(new Inventory("Hammers", 3.50, 4));

    inv.Add(new Inventory("Drills", 19.88, 8));

    Console.WriteLine("Inventory list before sorting:");

    foreach(Inventory i in inv) {

      Console.WriteLine("   " + i);

    }


    Console.WriteLine();

www.freepdf-books.com




808

 

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

    // Sort the list using an IComparer.

    inv.Sort(comp);

    Console.WriteLine("Inventory list after sorting:");

    foreach(Inventory i in inv) {

      Console.WriteLine("   " + i);

    }

  }


}


Download 4,07 Mb.

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