Der er ikke ret meget magi i events.
Eksempel:
using System;
namespace E
{
public delegate void D(string msg);
public class Demo
{
public event D E;
public void M1()
{
E("M1 called");
}
public void M2()
{
E("M2 called");
}
}
public class Program
{
public static void ConsolePrint(string msg)
{
Console.WriteLine(msg);
}
public static void ConsolePrintSpecial(string msg)
{
Console.WriteLine(">" + msg.ToUpper() + "<");
}
public static void Main(string[] args)
{
Demo o = new Demo();
o.E += ConsolePrint;
o.M1();
o.M2();
o.E += ConsolePrintSpecial;
o.M1();
o.M2();
o.E -= ConsolePrint;
o.M1();
o.M2();
Console.ReadKey();
}
}
}
Saa du kan sagtens bruge events tiul de ting du naevner, men du skal selv skrive koden som kalder event.