views:

48

answers:

3
public delegate void ExecuteCallback();

class Executioner { private ExecuteCallback _doExecute;

public void AddMultiple()
{
    // Add a delegate to MethodA
    // This will work even if _doExecute is currently null
    _doExecute += new Execute( MethodA );

    // Add a delegate to MethodB also
    _doExecute += new Execute( MethodB );

    // Add a delegate to MethodC also
    _doExecute += new Execute( MethodC );
}

public void MethodA()
{
    //...
}

public void MethodB()
{
    //...
}

public void MethodC()
{
    //...
}

}

+2  A: 

This is just a shorthand provided by C# for calling Delegate.Combine. It also works for events, where it calls the subscription part of the event (the add {} block in a C# event declaration, for example).

I don't believe there's a particular name for combination - it's just the binary + and += operators, from sections 7.8.4 and 7.17.2 of the C# spec, respectively.

Jon Skeet
+2  A: 

+= is usually called the "addition assignment operator" but in the context of delegates...

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.

Taken from MSDN documentation here.

How to: Subscribe to and Unsubscribe from Events

Daniel Renshaw
Note that the handling of events and delegates is quite different.
Jon Skeet
A: 

I would probably refer to that as 'registering' the event handler with the delegate / event. Depending on context.

BombDefused