After reading this question , it seems that 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 fine (and does nothing). How does this code behave 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: Catch this exception, according to Lasse W. Carlsen:
private void Form1_Load(object sender, EventArgs e) { try { EventHandler myHandler = null; myHandler(this, null); } catch (Exception ex) { this.Text = "Exception!"; } }
source share