Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this event?

Could someone explain what this C# code is doing?

// launch the camera capture when the user touch the screen
this.MouseLeftButtonUp += (s, e) => new CameraCaptureTask().Show();

// this static event is raised when a task completes its job
ChooserListener.ChooserCompleted += (s, e) =>
{
    //some code here
};

I know that CameraCaptureTask is a class and has a public method Show(). What kind of event is this? what is (s, e)?

like image 660
Shawn Mclean Avatar asked May 16 '10 07:05

Shawn Mclean


People also ask

What it is an event?

noun. something that happens or is regarded as happening; an occurrence, especially one of some importance. the outcome, issue, or result of anything: The venture had no successful event. something that occurs in a certain place during a particular interval of time.

What is event short answer?

An event is something that happens, especially when it is unusual or important. You can use events to describe all the things that are happening in a particular situation.

What is an event example?

What is an Event? In probability, the set of outcomes from an experiment is known as an Event. So say for example you conduct an experiment by tossing a coin. The outcome of this experiment is the coin landing 'heads' or 'tails'.

What is a current event?

Definition of current events : important events that are happening in the world She reads several newspapers so she can keep track of current events.


2 Answers

When attaching event handlers, you can do in three different ways:

The old fashioned verbose way:

this.MouseLeftButtonUp += Handle_MouseLeftButtonUp;
void Handle_MouseLeftButtonUp(object s, MouseButtonEventArgs e)
{
  new CameraCaptureTask().Show(); 
}

An anonymous method:

this.MouseLeftButtonUp += delegate(object s, MouseButtonEventArgs e) {
  new CameraCaptureTask().Show(); 
}

Or, using a lambda expression:

this.MouseLeftButtonUp += (s, e) => new CameraCaptureTask().Show(); 

Imagine the last one as a 'compact form' of the one using the delegate. You could also use braces:

this.MouseLeftButtonUp += (s, e) => {
  new CameraCaptureTask().Show(); 
}
like image 83
Fernando Avatar answered Sep 30 '22 20:09

Fernando


(s, e) => new CameraCaptureTask().Show();

This is an anonymous delegate (lambda expression). This takes 2 parameters (s and e (which are unused)), and then create a new CameraCaptureTask and show it.

like image 21
kennytm Avatar answered Sep 30 '22 21:09

kennytm