C# 0 The Complete Reference


Main( ) , four delegate instances are created. One,  strOp



Download 4,07 Mb.
Pdf ko'rish
bet516/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   512   513   514   515   516   517   518   519   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Main( )

, four delegate instances are created. One, 

strOp

, is null. The other three refer to 

specific string modification methods. Next, a multicast is created that calls 

RemoveSpaces( )

and


Reverse( )

. This is accomplished via the following lines:

strOp = replaceSp;

strOp += reverseStr;

First,

strOp

 is assigned 



replaceSp

. Next, using 



+=

,

reverseStr

 is added. When 

strOp

 is 


invoked, both methods are invoked, replacing spaces with hyphens and reversing the 

string, as the output illustrates.

Next,

replaceSp

 is removed from the chain, using this line:

strOp -= replaceSp;

and


removeSP

 is added using this line:

strOp += removeSp;

Then,


strOp

 is again invoked. This time, spaces are removed and the string is reversed.

Delegate chains are a powerful mechanism because they allow you to define a set of 

methods that can be executed as a unit. This can increase the structure of some types of 

code. Also, as you will soon see, delegate chains have a special value to events.

Covariance and Contravariance

There are two features that add flexibility to delegates: 



covariance

and


contravariance.

Normally, 

the method that you pass to a delegate must have the same return type and signature as the 

delegate. However, covariance and contravariance relax this rule slightly, as it pertains to 

derived types. Covariance enables a method to be assigned to a delegate when the method’s 

return type is a class derived from the class specified by the return type of the delegate. 

Contravariance enables a method to be assigned to a delegate when a method’s parameter 

type is a base class of the class specified by the delegate’s declaration.

Here is an example that illustrates both covariance and contravariance:

// Demonstrate covariance and contravariance.

using System;

class X {

  public int Val;

}

// Y is derived from X.



class Y : X { }

// This delegate returns X and takes a Y argument.

delegate X ChangeIt(Y obj);

class CoContraVariance {

  // This method returns X and has an X parameter.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   512   513   514   515   516   517   518   519   ...   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