After reading this question, it seems like the following code should fail:
private void Form1_Load(object sender, EventArgs e)
{
EventHandler myHandler = null;
myHandler(this, null);
}
But when I run it, it works just fine (and does nothing). How does that code behave any differently than the following?
private void Form1_Load(object sender, EventArgs e)
{
EventHandler myHandler = null;
EventHandler myCopy = myHandler;
if (myCopy != null)
{
myHandler(this, null);
}
}
Edit: Catching the exception this way works, according to Lasse V. Karlsen's answer:
private void Form1_Load(object sender, EventArgs e)
{
try
{
EventHandler myHandler = null;
myHandler(this, null);
}
catch (Exception ex)
{
this.Text = "Exception!";
}
}