C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet670/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   666   667   668   669   670   671   672   673   ...   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  

537


PART IPART I

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

  public int CompareTo(MyClass obj) {

    return Val - obj.Val; // Now, no cast is needed.

  }

}

Notice that a cast is no longer required by this line in 



CompareTo( )

:

return Val - obj.Val; // Now, no cast is needed.



Because the type parameter to 

IComparable

 is 


MyClass

, the type of 



obj

 is now known to be 



MyClass

.

Here is an updated version of 



IsIn( )

 that requires 



IComparable

:

// 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;

}

N



OTE

N

OTE

If a type parameter specifies a reference or a base class constraint, then 

= =

 and 

! =

 can be 

applied to instances of that type parameter, but they only test for reference equality. To compare 

values, you must implement 

IComparable

 or 

IComparable

.

Generic Class Hierarchies

Generic classes can be part of a class hierarchy in just the same way as non-generic classes. 

Thus, a generic class can act as a base class or be a derived class. The key difference between 

generic and non-generic hierarchies is that in a generic hierarchy, any type arguments needed 

by a generic base class must be passed up the hierarchy by all derived classes. This is similar 

to the way that constructor arguments must be passed up a hierarchy.



Using a Generic Base Class

Here is a simple example of a hierarchy that uses a generic base class:

// A simple generic class hierarchy.

using System;

// A generic base class.

class Gen {

  T ob;

  public Gen(T o) {

    ob = o;

  }


  // Return ob.

  public T GetOb() {

www.freepdf-books.com



538

 

P a r t   I :  



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

    return ob;

  }

}

// A class derived from Gen.



class Gen2 : Gen {

  public Gen2(T o) : base(o) {

    // ...

  }


}

class GenHierDemo {

  static void Main() {

    Gen2 g2 = new Gen2("Hello");

    Console.WriteLine(g2.GetOb());

  }


}

In this hierarchy, 



Gen2

 inherits the generic class 



Gen

. Notice how 



Gen2

 is declared by 

the following line:

class Gen2 : Gen {

The type parameter 

T

 is specified by 



Gen2

 and is also passed to 



Gen

. This means that 

whatever type is passed to 

Gen2

 will also be passed to 



Gen

. For example, this declaration

Gen2 g2 = new Gen2("Hello");

passes


string

 as the type parameter to 



Gen

. Thus, the 



ob

 inside the 



Gen

 portion of 



Gen2

will be of type 



string

.

Notice also that 



Gen2

 does not use the type parameter 



T

 except to pass it along to the 



Gen

 base class. Thus, even if a derived class would otherwise not need to be generic, it still 

must specify the type parameter(s) required by its generic base class.

Of course, a derived class is free to add its own type parameters, if needed. For example, 

here is a variation on the preceding hierarchy in which 

Gen2

 adds a type parameter of its 

own:

// A derived class can add its own type parameters.



using System;

// A generic base class.

class Gen {

  T ob; // declare a variable of type T

  // Pass the constructor a reference of type T.

  public Gen(T o) {

    ob = o;

  }


  // Return ob.

  public T GetOb() {

    return ob;

www.freepdf-books.com




Download 4,07 Mb.

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