C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet668/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   664   665   666   667   668   669   670   671   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 8 :  

G e n e r i c s  

535


PART IPART I

    if(v.CompareTo(what) == 0) // now OK, uses CompareTo()

      return true;

  return false;

}

Notice the use of the constraint



where T : IComparable

This constraint ensures that only types that implement 



IComparable

 are valid type 

arguments for 

IsIn( )

.

The following program demonstrates 



IsIn( )

. It also shows how 



IComparable

 can be 


easily implemented by a class:

// Demonstrate IComparable.

using System;

class MyClass : IComparable {

  public int Val;

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

  // Implement IComparable.

  public int CompareTo(object obj) {

    return Val - ((MyClass) obj).Val;

  }


}

class CompareDemo {

  // Require IComparable interface.

  public static bool IsIn(T what, T[] obs) where T : IComparable {

    foreach(T v in obs)

      if(v.CompareTo(what) == 0) // now OK, uses CompareTo()

        return true;

    return false;

  }

  // Demonstrate comparisons.



  static void Main() {

    // Use IsIn() with int.

    int[] nums = { 1, 2, 3, 4, 5 };

    if(IsIn(2, nums))

      Console.WriteLine("2 is found.");

    if(IsIn(99, nums))

      Console.WriteLine("This won’t display.");

    // Use IsIn() with string.

www.freepdf-books.com



536

 

P a r t   I :  



T h e   C #   L a n g u a g e

    string[] strs = { "one", "two", "Three"};

    if(IsIn("two", strs))

      Console.WriteLine("two is found.");

    if(IsIn("five", strs))

      Console.WriteLine("This won’t display.");

    // Use IsIn with MyClass.

    MyClass[] mcs = { new MyClass(1), new MyClass(2),

                      new MyClass(3), new MyClass(4) };

    if(IsIn(new MyClass(3), mcs))

      Console.WriteLine("MyClass(3) is found.");

    if(IsIn(new MyClass(99), mcs))

      Console.WriteLine("This won’t display.");

  }


}

The output is shown here:

2 is found.

two is found.

MyClass(3) is found.

Although the preceding program is correct, there is still one potential trouble spot. 

Notice how 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   664   665   666   667   668   669   670   671   ...   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