One of the nicest things about the new anonymous methods is how simple event handling has become. Here is an example of how we used to handle events. It demonstrates how to add an event handler to the btnSave’s Click event and assign a method to handle it.

protected void Page_Load(object sender, EventArgs e)

{

  btnSave.Click += new EventHandler(btnSave_Click);

}

 

void btnSave_Click(object sender, EventArgs e)

{

  DoSomething();
}

The next approach uses anonymous method to do exactly the same thing.

protected void Page_Load(object sender, EventArgs e)

{

  btnSave.Click += delegate { DoSomething(); };
}

This will save a lot of code lines.

Comments


Comments are closed