1

I have the following code:

recognizer = new GestureRecognizer();
recognizer.Tapped += (args) =>
{
    // Send an OnSelect message to the focused object and its ancestors.
    if (FocusedObject != null)
    {
        FocusedObject.SendMessageUpwards("OnSelect", 
            SendMessageOptions.DontRequireReceiver);
    }
};

I am working in a project for Unity and I am not that experienced with the syntax of this operation:

recognizer.Tapped += (args) =>
{
    // Send an OnSelect message to the focused object and its ancestors.
    if (FocusedObject != null)
    {
        FocusedObject.SendMessageUpwards("OnSelect", 
            SendMessageOptions.DontRequireReceiver);
    }

I understand that Tapped is the event listener for taps. However, I don't understand the following things:

1) Why are they using a += operator to add a lambda function. How can you add a lambda function like that? I've always used += on primitive types.

2) Where does "args" come from?

3) When does that lambda function run?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
ptt
  • 113
  • 1
  • 7
  • 2
    This isn't adding a lambda function. `+=` is the syntax that adds an event listener. A lambda can be an event listener as long as it accepts the event's arguments – Panagiotis Kanavos Nov 14 '17 at 18:23
  • @PanagiotisKanavos is Tapped an array? How does the += operator allow this? So it is adding a function to serve as an event listener. – ptt Nov 14 '17 at 18:25
  • 1
    It's an *event*, not an array. `+=` is the syntax used to add event listeners to events. [It's explained in the docs](https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/). That's not new syntax either, I think this was available since .NET 1.0 – Panagiotis Kanavos Nov 14 '17 at 18:28
  • It's worth noting that you are mixing in two concepts. The first is event registration, the second is lambda expressions. It is not a requirement that events subscribe to lambda expressions, you could just as easily supply the name of a method whose signature matches what is expected, which in this specific case is a void method which takes a single argument in. – myermian Nov 14 '17 at 18:37
  • @Pablo - Did any of the answer below help you solve the problem? – Gilad Green Nov 14 '17 at 20:01

2 Answers2

4

Why are they using a += operator to add a lambda function?

The point here is not the lambda method but the event and registering delegates to it. It overrloads the += operator to subscribe more event listeners. The Tapped property is an event (see links below).

From the documentation of the += operator:

The += operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. The use of the += operator in this context is referred to as subscribing to an event.

Where does "args" come from?

When the event triggers it triggers with an argument (In this specific case of the definition of Tapped. When it does it passes this argument to all the event listeners.

When does that lambda function run?

Whenever the event is triggered and causes all the listeners to be called. One of them will be this lambda method.


I suggest have a look at: How to: Subscribe to and Unsubscribe from Events and more in general about events

Also have a look at Delegates and Events on MSDN and += operator with Events

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
0

Basically, it is a lambda event listener. Rather than define it as a function like:

function void myEvent(object o, EventArgs args){}; recognizer.Tapped += myEvent;

It's defined and assigned in the same place.

The reason that += is used is because you can have multiple event handlers assigned to the same event like so:

function void myEvent(object o, EventArgs args){};
function void yourEvent(object o, EventArgs args){};
recognizer.Tapped += myEvent;
recognizer.Tapped += yourEvent;

If you wanted to remove the assigned event you could use -= like so:

recognizer.Tapped -= myEvent;

The args are part of the event handler definition that is requited for all events

Erick
  • 1,176
  • 15
  • 25