// A very simple event demonstration.
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();
}
// An event handler.
public static void handler()
{
Console.WriteLine("Event occurred");
}
}
class EventDemo
{
static void handler2()
{
Console.WriteLine("Event occurred 2");
}
public static void Main()
{
MyEvent evt = new MyEvent();
// Add handler() to the event list.
evt.SomeEvent += MyEvent.handler; // use method group conversion
evt.SomeEvent += handler2;
// evt.SomeEvent -= handler2;
// Fire the event.
evt.OnSomeEvent();
}
}
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();
}
// An event handler.
public static void handler()
{
Console.WriteLine("Event occurred");
}
}
class EventDemo
{
static void handler2()
{
Console.WriteLine("Event occurred 2");
}
public static void Main()
{
MyEvent evt = new MyEvent();
// Add handler() to the event list.
evt.SomeEvent += MyEvent.handler; // use method group conversion
evt.SomeEvent += handler2;
// evt.SomeEvent -= handler2;
// Fire the event.
evt.OnSomeEvent();
}
}