Skip to content

Commit c788481

Browse files
pkulikovBillWagner
authored andcommitted
Delegate removal: added remarks about equality (#12636)
1 parent 5a4e2c2 commit c788481

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/csharp/language-reference/operators/addition-operator.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ For operands of the same [delegate](../keywords/delegate.md) type, the `+` opera
3737

3838
[!code-csharp-interactive[delegate combination](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddDelegates)]
3939

40+
To perform delegate removal, use the [`-` operator](subtraction-operator.md#delegate-removal).
41+
4042
For more information about delegate types, see [Delegates](../../programming-guide/delegates/index.md).
4143

4244
## Addition assignment operator +=

docs/csharp/language-reference/operators/subtraction-operator.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ For information about the arithmetic `-` operator, see the [Unary plus and minus
2525
For operands of the same [delegate](../keywords/delegate.md) type, the `-` operator returns a delegate instance that is calculated as follows:
2626

2727
- If both operands are non-null and the invocation list of the second operand is a proper contiguous sublist of the invocation list of the first operand, the result of the operation is a new invocation list that is obtained by removing the second operand's entries from the invocation list of the first operand. If the second operand's list matches multiple contiguous sublists in the first operand's list, only the right-most matching sublist is removed. If removal results in an empty list, the result is `null`.
28-
- If the invocation list of the second operand is not a proper contiguous sublist of the invocation list of the first operand, the result of the operation is the first operand.
28+
29+
[!code-csharp-interactive[delegate removal](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)]
30+
31+
- If the invocation list of the second operand is not a proper contiguous sublist of the invocation list of the first operand, the result of the operation is the first operand. For example, removing a delegate that is not part of the multicast delegate does nothing and results in the unchanged multicast delegate.
32+
33+
[!code-csharp-interactive[delegate removal with no effect](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemovalNoChange)]
34+
35+
The preceding example also demonstrates that during delegate removal delegate instances are compared. For example, delegates that are produced from evaluation of identical [lambda expressions](../../programming-guide/statements-expressions-operators/lambda-expressions.md) are not equal. For more information about delegate equality, see the [Delegate equality operators](~/_csharplang/spec/expressions.md#delegate-equality-operators) section of the [C# language specification](../language-specification/index.md).
36+
2937
- If the first operand is `null`, the result of the operation is `null`. If the second operand is `null`, the result of the operation is the first operand.
3038

31-
The following example shows how the `-` operation performs delegate removal:
39+
[!code-csharp-interactive[delegate removal and null](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemovalAndNull)]
40+
41+
To combine delegates, use the [`+` operator](addition-operator.md#delegate-combination).
3242

33-
[!code-csharp-interactive[delegate removal](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)]
43+
For more information about delegate types, see [Delegates](../../programming-guide/delegates/index.md).
3444

3545
## Subtraction assignment operator -=
3646

0 commit comments

Comments
 (0)