static void Main()
{
ExecuteCommand();
Debug.Assert(CommandExecuted == null);
ExecuteCommandLeaks();
//This assert will fail
Debug.Assert(CommandExecuted == null);
}
static void ExecuteCommandLeaks()
{
CommandExecuted += new EventHandler<EventArgs>((o, e) => Console.WriteLine("Event called"));
CommandExecuted.Invoke(new object(), new EventArgs());
}
static void ExecuteCommand()
{
var commandCapture = new EventHandler<EventArgs>((o, e) => Console.WriteLine("Event called"));
CommandExecuted += commandCapture;
CommandExecuted.Invoke(new object(), new EventArgs());
CommandExecuted -= commandCapture;
}
static EventHandler<EventArgs> CommandExecuted;