2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 16:49:39 +02:00
|
|
|
In C#, delegates can be added together to chain their execution, and subtracted to remove their execution from the chain.
|
|
|
|
|
|
|
|
|
|
|
|
Subtracting a chain of delegates from another one might yield unexpected results as shown hereunder - and is likely to be a bug.
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
MyDelegate first, second, third, fourth;
|
|
|
|
first = () => Console.Write("1");
|
|
|
|
second = () => Console.Write("2");
|
|
|
|
third = () => Console.Write("3");
|
|
|
|
fourth = () => Console.Write("4");
|
|
|
|
|
|
|
|
MyDelegate chain1234 = first + second + third + fourth; // Compliant - chain sequence = "1234"
|
|
|
|
MyDelegate chain12 = chain1234 - third - fourth; // Compliant - chain sequence = "12"
|
|
|
|
|
|
|
|
|
|
|
|
MyDelegate chain14 = first + fourth; // creates a new MyDelegate instance which is a list under the covers
|
|
|
|
MyDelegate chain23 = chain1234 - chain14; // Noncompliant; (first + fourth) doesn't exist in chain1234
|
|
|
|
|
|
|
|
|
|
|
|
// The chain sequence of "chain23" will be "1234" instead of "23"!
|
|
|
|
// Indeed, the sequence "1234" does not contain the subsequence "14", so nothing is subtracted
|
|
|
|
// (but note that "1234" contains both the "1" and "4" subsequences)
|
|
|
|
chain23 = chain1234 - (first + fourth); // Noncompliant
|
|
|
|
|
|
|
|
chain23(); // will print "1234"!
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
MyDelegate chain23 = chain1234 - first - fourth; // Compliant - "1" is first removed, followed by "4"
|
|
|
|
|
|
|
|
chain23(); // will print "23"
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Review this subtraction of a chain of delegates: it may not work as you expect.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 30 Jun 2015, 16:12:38 Ann Campbell wrote:
|
|
|
|
\[~tamas.vajk] I've used a mashup of code samples. Please double-check my work
|
|
|
|
|
|
|
|
=== on 1 Jul 2015, 07:22:03 Tamas Vajk wrote:
|
|
|
|
\[~ann.campbell.2] Added a few more lines to the sample, but otherwise it looks good.
|
|
|
|
|
|
|
|
|
|
|
|
Note: delegate subtraction is not always a bug, so I'm not sure if we can apply the _bug_ label.
|
|
|
|
|
|
|
|
=== on 1 Jul 2015, 11:28:15 Ann Campbell wrote:
|
|
|
|
label updated [~tamas.vajk]
|
|
|
|
|
|
|
|
=== on 26 Jul 2016, 19:48:47 Ann Campbell wrote:
|
|
|
|
\[~tamas.vajk] I think we need to work on this one.
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|