C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet536/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   532   533   534   535   536   537   538   539   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

X

 and 


Y

, which also define event 

handlers compatible with 

MyEventHandler

. Thus, these handlers can also become part 

of the event chain. Notice that the handlers in 

X

 and 


Y

 are not 



static

. This means that objects 

of each must be created, and the handler linked to each instance must be added to the event 

chain. The differences between instance and 



static

 handlers is examined in the next section.



Instance Methods vs. Static Methods as Event Handlers

Although both instance methods and 



static

 methods can be used as event handlers, they do 

differ in one important way. When a 

static

 method is used as a handler, an event notification 

applies to the class. When an instance method is used as an event handler, events are sent to 

specific object instances. Thus, each object of a class that wants to receive an event notification 

must register individually. In practice, most event handlers are instance methods, but, of 

course, this is subject to the specific application. Let’s look at an example of each.

The following program creates a class called 

X

 that defines an instance method as an 

event handler. This means that each 

X

 object must register individually to receive events. 

To demonstrate this fact, the program multicasts an event to three objects of type 

X

.

/* Individual objects receive notifications when instance



   event handlers are used. */

using System;

// Declare a delegate type for an event.

delegate void MyEventHandler();

// Declare a class that contains an event.

class MyEvent {

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   532   533   534   535   536   537   538   539   ...   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