Until now, I've been focusing on only one of the differences between delegates and function pointers; namely, associated state.
Delegates have one other capability that function pointers do not. A single function pointer can only point to one function. .Net, on the other hand, supports multicast delegates – delegates that point to multiple functions. You can combine two existing delegates using the + operator (or by calling Delegate.Combine) to create a single new delegate instance that points two all of the methods in the original two delegates. This new delegate stores all of the methods from the original two delegates in a private array of delegates called InvocationList (the delegates in this array are ordinary non-multicast delegates that each only point to a single method).
Note that delegates, like strings, are immutable. Adding two delegates together creates a third delegate containing the methods from the first two; the original delegate instances are not affected. For example, writing delegateField += SomeMethod
creates a new delegate instance containing the methods originally in delegateField
as well as SomeMethod
, then stores this new instance in delegateField
.
Similarly, the - operator (or Delegate.Remove) will remove the second operand from the first one (again, returning a new delegate instance). If the second operand has multiple methods, all of them will be removed from the final delegate. If some of the methods in the second operand appear multiple times in the original delegate, only the last occurrence of each one will be removed (the one most recently added). The RemoveAll method will remove all occurrences. If all of the methods were removed, it will return null; there is no such thing as an empty delegate instance.
Multicast delegates are not intended to be used with delegates that return values. If you call a non-void delegate that contains multiple methods, it will return the return value of the last method in the delegate. If you want to see the return values of all of the methods, you’ll need to loop over GetInvocationList() and call each delegate individually.
Multicast delegates also don’t play well with the new covariant and contravariant generic delegates in .Net 4.0. You cannot combine two delegates unless their types match exactly, including variant generic parameters.
Function pointers cannot easily be combined the way multicast delegates can. The only way to combine function pointers without cooperation from the code that calls the pointer is to make a function that uses a closure to call all of the function pointers you want to call.
In Javascript, that would look like this:
function combine() { var methods = arguments; return function() { var retVal; for(var i = 0; i < methods.length; i++) retVal = methods[i].apply(this, arguments); return retVal; }; }