Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the + operator work for combining delegates?

Tags:

c#

delegates

For example:

delegate void SomeDelegate();

SomeDelegate a = new SomeDelegate( () => Console.WriteLine("A") );
SomeDelegate b = new SomeDelegate( () => Console.WriteLine("B") );

SomeDelegate c = a + b;

In the last line, what does a + b translate to? I'm just curious how I would add them without using the + operator.

like image 345
Ryan Peschel Avatar asked Nov 25 '12 07:11

Ryan Peschel


People also ask

How do multicast delegates work?

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.

Which of the following operator is used to remove a delegate?

Multicasting of a Delegate The "-" operator can be used to remove a component delegate from a composed delegate.


2 Answers

http://msdn.microsoft.com/en-us/library/ms173172(v=VS.80).aspx - Search for addition:

A delegate can call more than one method when invoked. This is referred to as multicasting. To add an extra method to the delegate's list of methods—the invocation list—simply requires adding two delegates using the addition or addition assignment operators ('+' or '+='). For example:

MethodClass obj = new MethodClass(); 
Del d1 = obj.Method1; 
Del d2 = obj.Method2; 
Del d3 = DelegateMethod;

//Both types of assignment are valid. 
Del allMethodsDelegate = d1 + d2; 
allMethodsDelegate += d3;

At this point allMethodsDelegate contains three methods in its invocation list—Method1, Method2, and DelegateMethod. The original three delegates, d1, d2, and d3, remain unchanged. When allMethodsDelegate is invoked, all three methods are called in order. If the delegate uses reference parameters, the reference is passed sequentially to each of the three methods in turn, and any changes by one method are visible to the next method. When any of the methods throws an exception that is not caught within the method, that exception is passed to the caller of the delegate and no subsequent methods in the invocation list are called.

Update

Both delegates derive from System.Delegate You can use the combine() methods to add two delegates together.

like image 80
Prescott Avatar answered Oct 18 '22 00:10

Prescott


It is done using Delegate.Combine static method.

SomeDelegate c = Delegate.Combine(a, b) as SomeDelegate;

When using += operator it is just the same actually.

// This is the same...
eventSource.onEvent += OnEvent;

// ...as this.
eventSource.onEvent = Delegate.Combine(
    eventSource.onEvent,
    Delegate.CreateDelegate(typeof(EventSource.OnEvent), this, "OnEvent")
    ) as EventSource.OnEvent;

MulticastDelegate class (the class behind delegate keyword) do have a list of invocations, but this list is immutable. Each time you combine delegates with the += operator, a new MulticastDelegate instance get created combining the invocation list of the former two Delegate objects.

like image 42
Geri Borbás Avatar answered Oct 18 '22 00:10

Geri Borbás