C# 0 The Complete Reference


PART II C h a p t e r   2 1



Download 4,07 Mb.
Pdf ko'rish
bet805/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   801   802   803   804   805   806   807   808   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 1 :  

E x p l o r i n g   t h e   S y s t e m   N a m e s p a c e    

649


This method compares the invoking object against the value in 

obj.

 It returns greater than 

zero if the invoking object is greater than 

obj,

 zero if the two objects are equal, and less than 

zero if the invoking object is less than 

obj.

IComparable

 is the generic version of 



IComparable

. It defines the generic version 

of

CompareTo( )

:

int CompareTo(T 



obj

)

The generic version of 



CompareTo( )

 works like the non-generic version. It compares 

the invoking object against the value in 

obj.

 It returns greater than zero if the invoking object 

is greater than 

obj,

 zero if the two objects are equal, and less than zero if the invoking object 

is less than 

obj.

 The advantage of using 



IComparable

is type safety because the type of 

data being operated upon is explicitly specified. There is no need to cast the object being 

compared from 



object

into the desired type. Here is an example that illustrates sorting and 

searching an array of user-defined class objects:

// Sort and search an array of objects.

using System;

class MyClass : IComparable {

  public int i;

  public MyClass(int x) { i = x; }

  // Implement IComparable.

  public int CompareTo(MyClass v) {

    return i - v.i;

  }


  public bool Equals(MyClass v) {

    return i == v.i;

  }

}

class SortDemo {



  static void Main() {

    MyClass[] nums = new MyClass[5];

    nums[0] = new MyClass(5);

    nums[1] = new MyClass(2);

    nums[2] = new MyClass(3);

    nums[3] = new MyClass(4);

    nums[4] = new MyClass(1);

    // Display original order.

    Console.Write("Original order: ");

    foreach(MyClass o in nums)

      Console.Write(o.i + " ");

    Console.WriteLine();

    // Sort the array.

    Array.Sort(nums);

www.freepdf-books.com



650

 

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

    // Display sorted order.

    Console.Write("Sorted order:   ");

    foreach(MyClass o in nums)

      Console.Write(o.i + " ");

    Console.WriteLine();

    // Search for MyClass(2).

    MyClass x = new MyClass(2);

    int idx = Array.BinarySearch(nums, x);

    Console.WriteLine("Index of MyClass(2) is " + idx);

  }

}

The output is shown here:



Original order: 5 2 3 4 1

Sorted order:   1 2 3 4 5

Index of MyClass(2) is 1


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   801   802   803   804   805   806   807   808   ...   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