What is += operator doing in the provided C# code?

时间:2016-11-09 08:17:16

标签: c#

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;
    }
};

6 个答案:

答案 0 :(得分: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 .

答案 1 :(得分: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.

答案 2 :(得分: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!

答案 3 :(得分:1)

+=添加AND赋值运算符,它将右操作数添加到左操作数并将结果赋给左操作数。

在您的情况下,您指定一名代表来处理您的活动。但事件不是委托实例。

使用eventName += delegateInstance;eventName -= delegateInstance;分别在C#中调用add和remove方法,其中eventName可以使用引用(例如myForm.Click)或类型名称(例如MyClass.SomeEvent)进行限定。

简单来说,您正在使用按钮添加事件。因此TranslateButton = TranslateButton + Event将成为TranslateButton += Event

答案 4 :(得分: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;
        }
    }

答案 5 :(得分: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

相关问题