append/removeLast on c#delegate inv list

Note — inv list is like an ordered list. Order is the invocation order. Invocation is strictly sequential.

— Based on http://www.yoda.arachsys.com/csharp/events.html
Like java String, Delegate objects are Immutable — always. Seehttp://msdn.microsoft.com/en-us/library/system.delegate.aspx. Personally, I interpret this rule in terms of the invocation list. (a delegate object’s state is nothing but the inv list.)

Like java String, Delegate concatenation (implicitly using Delegate.Combine()) produces new objects, leaving original objects intact.

  d1 = d1+d2; // appends d2 to the end of d1, both are potentially lists of single delegates.
  d1 += d2; //same thing

Like java String, such assignments __Reseat__ d1 to point at the new object, keeping original objects immutable
Like java String, the “+” operator Appends to the end
Like java String, you can add [d1,d2] + [d2] to get [d1,d2,d2] with duplicates!
Unlike java String, you can Remove too. This illustration is worth 1000 words —

Expression Result
null + d1 d1
d1 + null d1
d1 + d2 [d1, d2]
d1 + [d2, d3] [d1, d2, d3]
[d1, d2] + [d2, d3] [d1, d2, d2, d3]
[d1, d2] – d1 d2
[d1, d2] – d2 d1
[d1, d2, d1] – d1 [d1, d2]
[d1, d2, d3] – [d1, d2] d3
[d1, d2, d3] – [d2, d1] [d1, d2, d3]
[d1, d2, d3, d1, d2] – [d1, d2] [d1, d2, d3]
[d1, d2] – [d1, d2] null

Here’s a bit of the official MSDN spec (adapted) —
The invocation list of a delegate instance is an ordered linked list of what I call “singles”. An invocation list can contain duplicate methods. During an invocation, methods are invoked in the order in which they appear in the invocation list. A delegate attempts to invoke every method in its invocation list; duplicates are invoked once for each time they appear in the invocation list. Delegates are immutable; once created, the invocation list of a delegate does not change.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s