This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// .NET delegates can also point to instance methods as well



Download 21,46 Mb.
Pdf ko'rish
bet691/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   687   688   689   690   691   692   693   694   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// .NET delegates can also point to instance methods as well.

SimpleMath m = new SimpleMath();

BinaryOp b = new BinaryOp(m.Add);

// Show information about this object.

DisplayDelegateInfo(b);

Console.WriteLine("10 + 10 is {0}", b(10, 10));

Console.ReadLine();

}

In this case, we would find the output shown in Figure 11-3.



Figure 11-3.

Examining a delegate’s invocation list (once again)



Source Code

The SimpleDelegate project is located under the Chapter 11 subdirectory.



Retrofitting the Car Type with Delegates

Clearly, the previous SimpleDelegate example was intended to be purely illustrative in nature, given

that there would be no reason to build a delegate simply to add two numbers. To provide a more

realistic use of delegate types, let’s retrofit the Car type created in Chapter 9 to send notifications

using .NET delegates rather than a custom callback interface. Beyond no longer implementing

IEngineNotification, here are the basic steps we will need to take:

• Define new delegate types that will send notifications to the caller.

• Declare a member variable of these delegate types in the Car class.

• Create helper functions on the Car that allow the caller to set the methods maintained by the

delegate member variables.

• Update the Accelerate() method to invoke the delegate’s invocation list under the correct

circumstances.

C H A P T E R   1 1  

D E L E G AT E S, E V E N T S, A N D   L A M B D A S



348

8849CH11.qxd  10/1/07  10:43 AM  Page 348




To begin, create a new Console Application project named CarDelegate and insert your

previous Car and Radio definitions from the CallbackInterface example of Chapter 9 (you may

wish to change the namespace containing these types to the current project name or import the

CallbackInterface namespace as an alternative). Consider the following updates to the Car class,

which address the first three points:

public class Car

{


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   687   688   689   690   691   692   693   694   ...   1830




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