C# 0 The Complete Reference


PART I C h a p t e r   1 5



Download 4,07 Mb.
Pdf ko'rish
bet537/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   533   534   535   536   537   538   539   540   ...   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  

423


PART IPART I

  public event MyEventHandler SomeEvent;

  // This is called to fire the event.

  public void OnSomeEvent() {

    if(SomeEvent != null)

      SomeEvent();

  }

}

class X {



  int id;

  public X(int x) { id = x; }

  // This is an instance method that will be used as an event handler.

  public void Xhandler() {

    Console.WriteLine("Event received by object " + id);

  }


}

class EventDemo3 {

  static void Main() {

    MyEvent evt = new MyEvent();

    X o1 = new X(1);

    X o2 = new X(2);

    X o3 = new X(3);

    evt.SomeEvent += o1.Xhandler;

    evt.SomeEvent += o2.Xhandler;

    evt.SomeEvent += o3.Xhandler;

    // Fire the event.

    evt.OnSomeEvent();

  }

}

The output from this program is shown here:



Event received by object 1

Event received by object 2

Event received by object 3

As the output shows, each object registers its interest in an event separately, and each 

receives a separate notification.

Alternatively, when a 



static

 method is used as an event handler, events are handled 

independently of any object, as the following program shows:

/* A class receives the notification when

   a static method is used as an event handler. */

using System;

// Declare a delegate type for an event.

www.freepdf-books.com




424

 

P a r t   I :  



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

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 {

  /* This is a static method that will be used as

     an event handler. */

  public static void Xhandler() {

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

  }

}

class EventDemo4 {



  static void Main() {

    MyEvent evt = new MyEvent();

    evt.SomeEvent += X.Xhandler;

    // Fire the event.

    evt.OnSomeEvent();

  }


}

The output from this program is shown here:

Event received by class.

In the program, notice that no object of type 




Download 4,07 Mb.

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