C # event handler info please

Can anyone explain what and how this code works / works?

RoleEnvironment.Chaning += RoleEnvironmentChaning;

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

      

basically if you could help me how event handling works in C # .net. Thank.

0


source to share


3 answers


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 you also don't want to constantly check if the user has clicked the button. What you want to do is run some custom code when the user eventually clicks the button.

Welcome to events or delegates.

Let's look at the button. Button has a Click event that you can hook up with your own code. i.e.

//This happens in the designer
Button button = new Button();
button.Click += new EventHandler(YourMethod);

      



Now your method will be called after the button is clicked.

What happens when a button is pressed? Someone will check the presence or absence of subscribers to the event

if(Click != null)
{
   Click(this, someEventArguments);
}

      

+2


source


Basically it's a saying: whenever RoleEnviroment decides to trigger an "change" event, call this method. (I assume this should be a change, not Chaning or Chanaing according to your code.)

In other words, events in C # are a publisher / subscriber or observer implementation .



For more information, see the article on Events and Delegates .

+2


source







All Articles