C# 0 The Complete Reference


StringOps . Multicasting



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

StringOps

.

Multicasting

One of the most exciting features of a delegate is its support for 

multicasting.

 In simple 

terms, multicasting is the ability to create an 

invocation list,

 or chain, of methods that will be 

automatically called when a delegate is invoked. Such a chain is very easy to create. Simply 

instantiate a delegate, and then use the 



+

 or 


+=

 operator to add methods to the chain. To 

remove a method, use 

 or 


– =

. If the delegate returns a value, then the value returned by 

the last method in the list becomes the return value of the entire delegate invocation. Thus, 

a delegate that makes use of multicasting will often have a 



void

 return type.

Here is an example of multicasting. Notice that it reworks the preceding examples by 

changing the string manipulation method’s return type to 



void

 and using a 



ref

 parameter to 

return the altered string to the caller. This makes the methods more appropriate for multicasting.

// Demonstrate multicasting.

using System;

// Declare a delegate type.

delegate void StrMod(ref string str);

class MultiCastDemo {

  // Replaces spaces with hyphens.

  static void ReplaceSpaces(ref string s) {

    Console.WriteLine("Replacing spaces with hyphens.");

    s = s.Replace(' ', '-');

  }

  // Remove spaces.



  static void RemoveSpaces(ref string s) {

    string temp = "";

    int i;

    Console.WriteLine("Removing spaces.");

    for(i=0; i < s.Length; i++)

      if(s[i] != ' ') temp += s[i];

    s = temp;

  }


www.freepdf-books.com


Download 4,07 Mb.

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