Q:

C# event handler explanation please

Could anyone please explain what and how this code below is doing/working?
RoleEnvironment.Chaning += RoleEnvironmentChaning;

private void RoleEnvironmentChanaing(object sender, RoleEnvironmentchaningnEventArgs e)
{
  ......
}

basically, if you could walk me through how event handling works in c#.net will be greatly appreciated.
Thanks.

A:

Let's forget about C# for a second and think about the following scenario.
You have a button on the screen that you want the user to click, you don't know when the user will click the button and neither do you want to check constantly if the user has clicked the button. What you want to do is run a bit of custom code when the user does eventually click on a button.
Welcome to events or delegates.
Let's take a look at the button.
The Button has a Click event that you can hook your custom code onto.
i.e.
//This happens in the designer
Button button = new Button();
button.Click += new EventHandler(YourMethod);

Your method will now be called once the button has been clicked.
What happens on Click of the button? Someone will check whether or not there are subscribers to the event
if(Click != null)
{
   Click(this, someEventArguments);
}

