Say I have the following C# code:
Action a = TestMethod;
Action b = TestMethod;
Action c = b;
b += a;
Tests indicates that b
is not the same instance as c
, so clearly the +
operator seems to create a new instance of the delegate. Is this a correct assumption? Does it reuse the b
-instance internally, or does it just copy the method/target information to a new instance?
I seem to be unable to find the implementation of the +
operator for delegates, the Delegate class doesn't seem to contain it.
Can anyone shed some light on this issue?
The reason I'm asking is that I'm creating some classes that will layer themselves around each other like onions, and the outermost layer will call an inner layer, obtain some delegates and then attach more methods to those delegates, I'm just concerned that doing so will in some way change the delegates I obtain from the inner layer, in which case I need to return copies/clones, instead of just the internal references.
Note that this is wholly internal code to my library, so while I know that passing out internal data structures like that is a bad idea generally, if I can do it safely with these classes, then I will do so.