C# 0 The Complete Reference


StrMod  takes one  string



Download 4,07 Mb.
Pdf ko'rish
bet512/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   508   509   510   511   512   513   514   515   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

StrMod

 takes one 



string

 parameter and returns a 



string

.

Next, in 



DelegateTest

, three 


static

 methods are declared, each with a single parameter 

of type 

string

 and a return type of 



string

. Thus, they match the 



StrMod

 delegate. These 

methods perform some type of string modification. Notice that 

ReplaceSpaces( )

 uses one 

of

string

’s methods, called 



Replace( )

, to replace spaces with hyphens.

In

Main( )

, a 


StrMod

 reference called 



strOp

 is created and assigned a reference to 



ReplaceSpaces( )

. Pay close attention to this line:

StrMod strOp = new StrMod(ReplaceSpaces);

Notice how the method 



ReplaceSpaces( )

 is passed as a parameter. Only its name is used; 

no parameters are specified. This can be generalized. When instantiating a delegate, you 

specify only the name of the method to which you want the delegate to refer. Of course, the 

method’s signature must match that of the delegate’s declaration. If it doesn’t, a compile-

time error will result.

Next,

ReplaceSpaces( )

 is called through the delegate instance 



strOp

, as shown here:

str = strOp("This is a test.");

Because


strOp

 refers to 



ReplaceSpaces( )

,

ReplaceSpaces( )

 is invoked.

www.freepdf-books.com




402

 

P a r t   I :  



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

Next,


strOp

 is assigned a reference to 



RemoveSpaces( )

, and then 



strOp

 is called again. 

This time, 

RemoveSpaces( )

 is invoked.

Finally, 

strOp

 is assigned a reference to 



Reverse( )

 and 


strOp

 is called. This results in 



Reverse( )

 being called.

The key point of the example is that the invocation of 

strOp

 results in a call to the method 

referred to by 

strOp

 at the time at which the invocation occurs. Thus, the method to call is 

resolved at runtime, not compile time.

Delegate Method Group Conversion

Beginning with version 2.0, C# has included an option that significantly simplifies the 

syntax that assigns a method to a delegate. This feature is called 

method group conversion,

and it allows you to simply assign the name of a method to a delegate, without using 



new

or explicitly invoking the delegate’s constructor.

For example, here is the 

Main( )

 method of the preceding program rewritten to use 

method group conversions:

static void Main() {

  // Construct a delegate using method group conversion.

  StrMod strOp = ReplaceSpaces; // use method group conversion

  string str;

  // Call methods through the delegate.

  str = strOp("This is a test.");

  Console.WriteLine("Resulting string: " + str);

  Console.WriteLine();

  strOp = RemoveSpaces; // use method group conversion

  str = strOp("This is a test.");

  Console.WriteLine("Resulting string: " + str);

  Console.WriteLine();

  strOp = Reverse; // use method group conversion

  str = strOp("This is a test.");

  Console.WriteLine("Resulting string: " + str);

}

Pay special attention to the way that 



strOp

 is created and assigned the method 



ReplaceSpaces

 in this line:

StrMod strOp = ReplaceSpaces; // use method group conversion

The name of the method is assigned directly to 



strOp

. C# automatically provides a 

conversion from the method to the delegate type. This syntax can be generalized to any 

situation in which a method is assigned to (or converted to) a delegate type.

Because the method group conversion syntax is simpler than the old approach, it is used 

throughout the remainder of this book.



Using Instance Methods as Delegates

Although the preceding example used 



static

 methods, a delegate can also refer to instance 

methods. It must do so, however, through an object reference. For example, here is a rewrite 

of the previous example, which encapsulates the string operations inside a class called 

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   508   509   510   511   512   513   514   515   ...   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