From 10f11b96e0b61e465c262eee71a9f5e7162b3a55 Mon Sep 17 00:00:00 2001 From: pkulikov Date: Mon, 3 Jun 2019 08:18:47 +0200 Subject: [PATCH] Delegate removal: added remarks about equality --- .../operators/addition-operator.md | 2 ++ .../operators/subtraction-operator.md | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/csharp/language-reference/operators/addition-operator.md b/docs/csharp/language-reference/operators/addition-operator.md index c615ab23dd63b..2401038806df4 100644 --- a/docs/csharp/language-reference/operators/addition-operator.md +++ b/docs/csharp/language-reference/operators/addition-operator.md @@ -37,6 +37,8 @@ For operands of the same [delegate](../keywords/delegate.md) type, the `+` opera [!code-csharp-interactive[delegate combination](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddDelegates)] +To perform delegate removal, use the [`-` operator](subtraction-operator.md#delegate-removal). + For more information about delegate types, see [Delegates](../../programming-guide/delegates/index.md). ## Addition assignment operator += diff --git a/docs/csharp/language-reference/operators/subtraction-operator.md b/docs/csharp/language-reference/operators/subtraction-operator.md index b7f442f54f3a5..282bd55dc673a 100644 --- a/docs/csharp/language-reference/operators/subtraction-operator.md +++ b/docs/csharp/language-reference/operators/subtraction-operator.md @@ -25,12 +25,22 @@ For information about the arithmetic `-` operator, see the [Unary plus and minus For operands of the same [delegate](../keywords/delegate.md) type, the `-` operator returns a delegate instance that is calculated as follows: - 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`. -- 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. + + [!code-csharp-interactive[delegate removal](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)] + +- 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. + + [!code-csharp-interactive[delegate removal with no effect](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemovalNoChange)] + + 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). + - 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. -The following example shows how the `-` operation performs delegate removal: + [!code-csharp-interactive[delegate removal and null](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemovalAndNull)] + +To combine delegates, use the [`+` operator](addition-operator.md#delegate-combination). -[!code-csharp-interactive[delegate removal](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)] +For more information about delegate types, see [Delegates](../../programming-guide/delegates/index.md). ## Subtraction assignment operator -=