C# 0 The Complete Reference


PhoneList  requires  that a type argument implement the  IPhoneList



Download 4,07 Mb.
Pdf ko'rish
bet644/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   640   641   642   643   644   645   646   647   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

PhoneList

 requires 

that a type argument implement the 

IPhoneList

 interface. Because both 



Friend

 and 


Supplier

implement



IPhoneList

, they are valid types to be bound to 



T

. However, 



EmailFriend

 does 


not implement 

IPhoneList

 and cannot be bound to 



T

. To prove this, remove the comment 

symbols from the last two lines in 

Main( )

. As you will see, the program will not compile.



Using the new( ) Constructor Constraint

The


new( )

 constructor constraint enables you to instantiate an object of a generic type. 

Normally, you cannot create an instance of a generic type parameter. However, the 

new( )

constraint changes this because it requires that a type argument supply a parameterless 

constructor. This can be the default constructor provided automatically when no explicit 

constructor is declared or a parameterless constructor explicitly defined by you. With the 



new( )

 constraint in place, you can invoke the parameterless constructor to create an object.

Here is a simple example that illustrates the use of 

new( )

:

// Demonstrate a new() constructor constraint.



using System;

class MyClass {

  public MyClass() {

    // ...

  }

  //...


}

class Test where T : new() {

  T obj;

  public Test() {

    // This works because of the new() constraint.

    obj = new T(); // create a T object

  }

  // ...


}

class ConsConstraintDemo {

  static void Main() {

    Test x = new Test();

  }

}

First, notice the declaration of the 



Test 

class, shown here:

class Test where T : new() {

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   640   641   642   643   644   645   646   647   ...   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