C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet635/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   631   632   633   634   635   636   637   638   ...   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  

505


PART IPART I

The base class constraint uses this form of the 



where

 clause:


where 

T

 : 


base-class-name

Here, 


T

 is the name of the type parameter, and 



base-class-name

 is the name of the base class. 

Only one base class can be specified.

Here is a simple example that demonstrates the base class constraint mechanism:

// A simple demonstration of a base class constraint.

using System;

class A {

  public void Hello() {

    Console.WriteLine("Hello");

  }


}

// Class B inherits A.

class B : A { }

// Class C does not inherit A.

class C { }

// Because of the base class constraint, all type arguments

// passed to Test must have A as a base class.

class Test where T : A {

  T obj;

  public Test(T o) {

    obj = o;

  }


  public void SayHello() {

    // OK to call Hello() because it’s declared

    // by the base class A.

    obj.Hello();

  }

}

class BaseClassConstraintDemo {



  static void Main() {

    A a = new A();

    B b = new B();

    C c = new C();

    // The following is valid because A is the specified base class.

    Test t1 = new Test(a);

    t1.SayHello();

    // The following is valid because B inherits A.

    Test t2 = new Test(b);

www.freepdf-books.com



1   ...   631   632   633   634   635   636   637   638   ...   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