C# 0 The Complete Reference


struct  or an  enum



Download 4,07 Mb.
Pdf ko'rish
bet650/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   646   647   648   649   650   651   652   653   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

struct

 or an 


enum

. (In this 

context, a nullable type is not considered a value type.) Here is an example:

// Demonstrate a value type constraint.

using System;

struct MyStruct {

  //...

}

class MyClass {



  // ...

}

class Test where T : struct {



  T obj;

  public Test(T x) {

    obj = x;

  }


  // ...

}

class ValueConstraintDemo {



  static void Main() {

    // Both of these declarations are legal.

    Test x = new Test(new MyStruct());

    Test y = new Test(10);

    // But, the following declaration is illegal!

//    Test z = new Test(new MyClass());

  }

}

In this program, 



Test

 is declared as shown here:

class Test where T : struct {

Because


T

 of 


Test

 now has the 



struct

 constraint, 



T

 can be bound only to value type arguments. 

This means that 

Test

 and 


Test

 are valid, but 



Test 

is not. To 

prove this, try removing the comment symbols from the start of the last line in the program 

and recompiling. An error will be reported.

www.freepdf-books.com



520

 

P a r t   I :  



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

Using a Constraint to Establish a Relationship Between Two Type Parameters

There is a variation of the base class constraint that allows you to establish a relationship 

between two type parameters. For example, consider the following generic class declaration:

class Gen where V : T {

In this declaration, the 

where

 clause tells the compiler that the type argument bound to 



V

must be identical to or inherit from the type argument bound to 



T

. If this relationship is not 

present when an object of type 

Gen

 is declared, then a compile-time error will result. A 

constraint that uses a type parameter, such as that just shown, is called a 

naked type constraint.

The following example illustrates this constraint:

// Create relationship between two type parameters.

using System;

class A {

  //...


}

class B : A {

  // ...

}

// Here, V must be or inherit from T.



class Gen where V : T {

  // ...


}

class NakedConstraintDemo {

  static void Main() {

    // This declaration is OK because B inherits A.

    Gen x = new Gen();

    // This declaration is in error because

    // A does not inherit B.

//    Gen y = new Gen();

  }

}

First, notice that class 



B

 inherits class 



A

. Next, examine the two 



Gen

 declarations in 



Main( )

. As the comments explain, the first declaration

Gen x = new Gen();

is legal because 



B

 inherits 



A

. However, the second declaration

//    Gen y = new Gen();

is illegal because 



A

 does not inherit 



B

.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   646   647   648   649   650   651   652   653   ...   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