I know that the += operator will add a method to the invocation list maintained by the Delegate base object, for example
using System; class Program { delegate void MyDelegate(int n); void Foo(int n) { Console.WriteLine("n = {0}", n) } static void Main(string[] args) { MyDelegate d = new MyDelegate(Foo); d += Foo; // add Foo again d.Invoke(3); // Foo is invoked twice as Foo appears two times in invocation list } }
But when I look at MSDN Delegate, MulticastDelegate I can't find any definition of the += operator. How is it that is just works? Auto-generated compiler magic?
Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.
C# provides special syntax for invoking a delegate. When a non- null delegate instance whose invocation list contains one entry, is invoked, it invokes the one method with the same arguments it was given, and returns the same value as the referred to method.
Delegates Overview Delegates are similar to C++ function pointers, but delegates are fully object-oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method. Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods.
Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application), the program would call a specific method.
It's not an operator on the delegate type itself, in IL terms - it's defined in the language specification, but you wouldn't find it using reflection. The compiler turns it into a call to Delegate.Combine
. The reverse operation, using -
or -=
, uses Delegate.Remove
.
At least, that's how it's implemented when C# targets .NET, as it almost always does. In theory, this is environment-specific - the language specification doesn't require that a compiler uses Delegate.Combine
or Delegate.Remove
, and a different environment may not have those methods.
From the C# 5 specification, section 7.8.4 (addition):
The binary
+
operator performs delegate combination when both operands are of some delegate typeD
. (If the operands have different delegate types, a binding-time error occurs.) If the first operand isnull
, the result of the operation is the value of the second operand (even if that is alsonull
). Otherwise, if the second operand isnull
, then the result of the operation is the value of the first operand. Otherwise, the result of the operation is a new delegate instance that, when invoked, invokes the first operand and then invokes the second operand. For examples of delegate combination, see §7.8.5 and §15.4. SinceSystem.Delegate
is not a delegate type, operator+
is not defined for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With