static event
In this chapter you will learn:
How to create static event
A class receives the notification when a static method is used as an event handler.
using System; /*from ja v a 2 s . c o m*/
delegate void MyEventHandler();
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();
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » delegate, lambda, event