Events are certain actions that happen during the execution of a program that the applications wishes to be notified about , so it can respond. An event can be a mouse click , a keystroke or the coming of a certain time ( alarm) . An event is basically a message which is said to be fired or triggered when the respective action occurs. A class that raises an event is called an ‘ event sender’ , a class that receives an event is called and ‘ event consumer’ and a method which is used to handle a particular event is called an ‘ even handler’.
Event Handling in C#
In .Net, events are implements as multicast delegates. In C# events are a first class ( basic ) language construct , and are defined using the event keyword. The steps for implementing events and event handling are :
- Define a public delegate for the event outside any class boundary. The conventional signature of a delegate for an event is :
public void EventDelegate ( object sender, EventArgs e)
2. Define a class to generate or raise the event. Define a public event in the class using the event keyword and the public delegate :
public event EventDelegate MyEvent
write some logic to raise the event . when raising an event , the first argument is usually the sender or originator of the event . The second argument is a sub- type of system. EvenArgs , which holds any additional data to be passed to the event handler.
class SomeEventArgs : EventArgs
An event is generally raised like this :
SomeEventArgs someData = new SomeEventArgs ( /* some necessary arguments */ );
Or if no data needs to be sent, the event is raised like this:
MyEvent ( this, null );
3. Defin e a class to receive the events. This class is usually the main application class containing the Main ( ) method Write an event handler method in the class. The signature of the event handler must be identical to that of the public delegate created in step 1 . The name of the event handler method conventionally starts with the word ” On “, e.g.
public void OnMyEvent ( object Sender , EventArgs e )
// handle the event
Instantiate the event generator class created in step 2 like this :
EventClass eventObj = new EventClass ( );
Add the event handler written in the current class to the event generator class ‘ event.
eventObj . MyEvent += new EventDelegate ( OnMyEvent);
Now the event handler ‘ OnMyEvent ( )’ will be called automatically whenever the event ‘ MyEvent ‘ is triggered.
Since events are implemented as multicast delegates in C#, we can subscribe multiple event handlers to a single event. For example, consider this revised Test class :
static void Main ( )
clockTimer clockTimer = new clockTimer ( );
clockTimer.Timer += new TimerEvent ( OnClockTick);
clockTimer.Timer += new TimerEvent ( OnClockTick2);
clockTimer.start ( );
public static void OnClockTick ( object sender, EventArgs e )
Console.WriteLine (” Received a clock tick event !”);
public static void OnClockTick2 ( object sender, EventArgs e)
Console.WriteLine (” Received a clock tick event in OnClockTick2 !”);