C# 0 The Complete Reference


PART I C h a p t e r   1 5



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


PART I

C h a p t e r   1 5 :  

D e l e g a t e s ,   E v e n t s ,   a n d   L a m b d a   E x p r e s s i o n s  

421


PART IPART I

Notice that the handler is added using the 



+=

 operator. Events support only 



+=

 and 


– =

. In 


this case, 

Handler( )

 is a 


static

 method, but event handlers can also be instance methods.

Finally, the event is fired as shown here:

// Fire the event.

evt.OnSomeEvent();

Calling


OnSomeEvent( )

 causes all registered event handlers to be called. In this case, there 

is only one registered handler, but there could be more, as the next section explains.

A Multicast Event Example

Like delegates, events can be multicast. This enables multiple objects to respond to an event 

notification. Here is an event multicast example:

// An event multicast demonstration.

using System;

// Declare a delegate type for an event.

delegate void MyEventHandler();

// Declare a class that contains an event.

class MyEvent {

  public event MyEventHandler SomeEvent;

  // This is called to fire the event.

  public void OnSomeEvent() {

    if(SomeEvent != null)

      SomeEvent();

  }

}

class X {



  public void Xhandler() {

    Console.WriteLine("Event received by X object");

  }

}

class Y {



  public void Yhandler() {

    Console.WriteLine("Event received by Y object");

  }

}

class EventDemo2 {



  static void Handler() {

    Console.WriteLine("Event received by EventDemo");

  }

  static void Main() {



    MyEvent evt = new MyEvent();

    X xOb = new X();

    Y yOb = new Y();

www.freepdf-books.com




422

 

P a r t   I :  



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

    // Add handlers to the event list.

    evt.SomeEvent += Handler;

    evt.SomeEvent += xOb.Xhandler;

    evt.SomeEvent += yOb.Yhandler;

    // Fire the event.

    evt.OnSomeEvent();

    Console.WriteLine();

    // Remove a handler.

    evt.SomeEvent -= xOb.Xhandler;

    evt.OnSomeEvent();

  }


}

The output from the program is shown here:

Event received by EventDemo

Event received by X object

Event received by Y object

Event received by EventDemo

Event received by Y object

This example creates two additional classes, called 




Download 4,07 Mb.

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