Delegates vs. Function Pointers, Addendum: Multicast Delegates

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

2 comments:

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

I'm not sure what your stress is, and I am not sure that side stepping normal activities is your best bet, due to the scarce amount of data you have provided. But I can give you a few suggestions for every day stresses.
Many people do meditation, others work out, some engage in social actions with kin and/or friends in order to reconnect and move away from the cause of the stress. There are also those who will lovingly and wastefully pamper themselves with long soaks in a tub, scented candles, and relaxing music.
I will suggest that you assess and know what the root cause of your stress is. If you cannot ease the stress by kind why you are knowing-how it, then maybe you should form a plan and even discuss with others what type of practical events you might be able to take in sort to move away from the source of this stress, once and for every.
One of the best ways to keep away from stress when you are going through a hard time is to try to take the best care of yourself that you maybe can. Stay hydrated, eat well, and try to get sufficient sleep. I know that is easier said than done sometimes, but it really will help if you can do it.
Also, it really depends on what hard times you are going through. Right away I'm going through a cute rough patch for myself. My boyfriend died 5 weeks ago. There are sure things that I've let go, in order to stay myself from string too much. Clearly I'm not going to let my house get filthy, but if I cannot vacuum every day, will that really hurt my family? No. If we have to drag laundry out of the storage bin in its place of out of the closet or drawers, does that really matter? No. Letting go of some of the more minor things is freeing. @ cheap essay writing service

Post a Comment