Mega Code Archive

 
Categories / C# / Language Basics
 

A class receives the notification when a static method is used as an event handler

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* A class receives the notification when      a static method is used as an event handler. */     using System;    // Declare a delegate for an event.   delegate void MyEventHandler();    // Declare an event class.  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.");    }  }    public class EventDemo3 {    public static void Main() {       MyEvent evt = new MyEvent();        evt.SomeEvent += new MyEventHandler(X.Xhandler);        // Fire the event.      evt.OnSomeEvent();    }  }