0

What does this += operator means in this code, is it a lambda? I read the MSDN document for the lambda but didn't find any thing about this += operator, I will be thankful if someone explain it to me

translateButton.Click += (object sender, EventArgs e) =>
{
    translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
    if (String.IsNullOrWhiteSpace(translatedNumber))
    {
        callButton.Text = "Call";
        callButton.Enabled = false;
    }
    else
    {
        callButton.Text = "Call " + translatedNumber;
        callButton.Enabled = true;
    }
};
Filip Cornelissen
  • 3,682
  • 3
  • 31
  • 41
Reza Najafi
  • 141
  • 1
  • 11

6 Answers6

6

+= adds an event handler to the Click event of translateButton object. See also this article at MSDN: https://msdn.microsoft.com/en-us/library/ms366768.aspx .

Andrew Sklyarevsky
  • 2,095
  • 14
  • 17
3

As an arithmetic operation +=/-= mean

add/subtract the right side to/from the left side and assign the result to the left side.

So writing a += 5 is the same as writing a = a + 5.

This code, however, is about events and event handlers. In the context of event handlers, +=/-= mean

add/remove the following delegate to/from the list of event handlers for this event.

So your sample code adds a new event handler to the button's Click event. The way it is written is called inline implementation.

Please note that in the context of event handlers, you can usually not replace

Event += Handler;

by

Event = Event + Handler;

as there is no way to "read" the Event "property" outside the implementing class.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

In order to subscribe a method (Anonymous or named) to that event you use this syntax:

translateButton.Click +=  (object sender, EventArgs e) => { /* .. Code*/ }

Or

translateButton.Click += SomeMethodThatMatchesSignature;

private void SomeMethodThatMatchesSignature(object sender, EventArgs e)
{
  // .. Code
}

Note that if you += two methods, both are going to be executed. You can remove one by use -= syntax.

In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class.

Delegates with more than one method in their invocation list derive from MulticastDelegate. Multicast delegates are used extensively in event handling

Read More About Delegates Here.

Side-Note: This is not by any mean specific to Xamarin!

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
1

+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand.

In your case you are assigning an delegate to handle your event. but Events aren't delegate instances.

The add and remove methods are called in C# using eventName += delegateInstance; and eventName -= delegateInstance; respectively, where eventName may be qualified with a reference (e.g. myForm.Click) or a type name (e.g. MyClass.SomeEvent).

In simple terms you are adding an event with the button. So TranslateButton = TranslateButton + Event will become TranslateButton += Event

Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

With += you add an event handler to the event.

The code that you showed could also be written like this:

    translateButton.Click += translateButton_Click;

    private void translateButton_Click(object sender, EventArgs e)
    {
        translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
        if (String.IsNullOrWhiteSpace(translatedNumber))
        {
            callButton.Text = "Call";
            callButton.Enabled = false;
        }
        else
        {
            callButton.Text = "Call " + translatedNumber;
            callButton.Enabled = true;
        }
    }
SubqueryCrunch
  • 1,325
  • 11
  • 17
0

It adds a handler to the Click event. And the handler(s) will be executed in order of adding.

You can read the documentation here

Niyoko
  • 7,512
  • 4
  • 32
  • 59