Skip to content

Commit cd1ccd7

Browse files
pkulikovBillWagner
authored andcommitted
Revised and merged the subtraction operator articles (#12541)
* Revised and merged the subtraction operator articles * Fix section link
1 parent b95f8e2 commit cd1ccd7

File tree

8 files changed

+64
-65
lines changed

8 files changed

+64
-65
lines changed

.openpublishing.redirection.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@
594594
"source_path": "docs/csharp/language-reference/operators/right-shift-operator.md",
595595
"redirect_url": "/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#right-shift-operator-"
596596
},
597+
{
598+
"source_path": "docs/csharp/language-reference/operators/subtraction-assignment-operator.md",
599+
"redirect_url": "/dotnet/csharp/language-reference/operators/arithmetic-operators#compound-assignment"
600+
},
597601
{
598602
"source_path": "docs/csharp/language-reference/operators/xor-assignment-operator.md",
599603
"redirect_url": "/dotnet/csharp/language-reference/operators/boolean-logical-operators#compound-assignment"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Starting with C# 6, [string interpolation](../tokens/interpolated.md) provides a
3333

3434
## Delegate combination
3535

36-
For [delegate](../keywords/delegate.md) types, the `+` operator returns a new delegate instance that, when invoked, invokes the first operand and then invokes the second operand. If any of the operands is `null`, the `+` operator returns the value of another operand (which also might be `null`). The following example shows how delegates can be combined with the `+` operator:
36+
For operands of the same [delegate](../keywords/delegate.md) type, the `+` operator returns a new delegate instance that, when invoked, invokes the first operand and then invokes the second operand. If any of the operands is `null`, the `+` operator returns the value of another operand (which also might be `null`). The following example shows how delegates can be combined with the `+` operator:
3737

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

@@ -80,3 +80,4 @@ For more information, see the [Unary plus operator](~/_csharplang/spec/expressio
8080
- [Events](../../programming-guide/events/index.md)
8181
- [Checked and unchecked](../keywords/checked-and-unchecked.md)
8282
- [Arithmetic operators](arithmetic-operators.md)
83+
- [- and -= operators](subtraction-operator.md)

docs/csharp/language-reference/operators/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ These operators have higher precedence than the next section and lower precedenc
193193

194194
[x += y](arithmetic-operators.md#compound-assignment) – increment. Add the value of `y` to the value of `x`, store the result in `x`, and return the new value. If `x` designates an [event](../keywords/event.md), then `y` must be an appropriate method that C# adds as an event handler.
195195

196-
[x -= y](subtraction-assignment-operator.md) – decrement. Subtract the value of `y` from the value of `x`, store the result in `x`, and return the new value. If `x` designates an [event](../keywords/event.md), then `y` must be an appropriate method that C# removes as an event handler.
196+
[x -= y](arithmetic-operators.md#compound-assignment) – decrement. Subtract the value of `y` from the value of `x`, store the result in `x`, and return the new value. If `x` designates an [event](../keywords/event.md), then `y` must be an appropriate method that C# removes as an event handler.
197197

198198
[x *= y](arithmetic-operators.md#compound-assignment) – multiplication assignment. Multiply the value of `y` to the value of `x`, store the result in `x`, and return the new value.
199199

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

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,74 @@
11
---
2-
title: "- operator - C# Reference"
2+
title: "- and -= operators - C# Reference"
33
ms.custom: seodec18
4-
5-
ms.date: 07/20/2015
4+
ms.date: 05/27/2019
65
f1_keywords:
76
- "-_CSharpKeyword"
7+
- "-=_CSharpKeyword"
88
helpviewer_keywords:
9+
- "subtraction operator [C#]"
10+
- "delegate removal [C#]"
911
- "- operator [C#]"
10-
- "subtraction operator (-) [C#]"
12+
- "subtraction assignment operator [C#]"
13+
- "event unsubscription [C#]"
14+
- "-= operator [C#]"
1115
ms.assetid: 4de7a4fa-c69d-48e6-aff1-3130af970b2d
1216
---
13-
# - operator (C# Reference)
17+
# - and -= operators (C# Reference)
18+
19+
The `-` operator is supported by the built-in numeric types and [delegate](../keywords/delegate.md) types.
20+
21+
For information about the arithmetic `-` operator, see the [Unary plus and minus operators](arithmetic-operators.md#unary-plus-and-minus-operators) and [Subtraction operator -](arithmetic-operators.md#subtraction-operator--) sections of the [Arithmetic operators](arithmetic-operators.md) article.
22+
23+
## Delegate removal
24+
25+
For operands of the same [delegate](../keywords/delegate.md) type, the `-` operator returns a delegate instance that is calculated as follows:
26+
27+
- 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.
29+
- 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.
30+
31+
The following example shows how the `-` operation performs delegate removal:
32+
33+
[!code-csharp-interactive[delegate removal](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)]
34+
35+
## Subtraction assignment operator -=
36+
37+
An expression using the `-=` operator, such as
38+
39+
```csharp
40+
x -= y
41+
```
42+
43+
is equivalent to
1444

15-
The `-` operator can function as either a unary or a binary operator.
45+
```csharp
46+
x = x - y
47+
```
1648

17-
## Remarks
49+
except that `x` is only evaluated once.
50+
51+
The following example demonstrates the usage of the `-=` operator:
1852

19-
Unary `-` operators are predefined for all numeric types. The result of a unary `-` operation on a numeric type is the numeric negation of the operand.
53+
[!code-csharp-interactive[-= examples](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#SubtractAndAssign)]
2054

21-
Binary `-` operators are predefined for all numeric and enumeration types to subtract the second operand from the first.
55+
You also use the `-=` operator to specify an event handler method to remove when you unsubscribe from an [event](../keywords/event.md). For more information, see [How to: subscribe to and unsubscribe from events](../../programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events.md).
2256

23-
Delegate types also provide a binary `-` operator, which performs delegate removal.
57+
## Operator overloadability
2458

25-
User-defined types can overload the unary `-` and binary `-` operators. For more information, see [operator keyword](../keywords/operator.md).
59+
A user-defined type can [overload](../keywords/operator.md) the `-` operator. When a binary `-` operator is overloaded, the `-=` operator is also implicitly overloaded. A user-defined type cannot explicitly overload the `-=` operator.
2660

27-
## Example
61+
## C# language specification
2862

29-
[!code-csharp[csRefOperators#40](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefOperators/CS/csrefOperators.cs#40)]
63+
For more information, see the [Unary minus operator](~/_csharplang/spec/expressions.md#unary-minus-operator) and [Subtraction operator](~/_csharplang/spec/expressions.md#subtraction-operator) sections of the [C# language specification](../language-specification/index.md).
3064

3165
## See also
3266

3367
- [C# Reference](../index.md)
3468
- [C# Programming Guide](../../programming-guide/index.md)
35-
- [C# operators](index.md)
69+
- [C# Operators](index.md)
70+
- [Delegates](../../programming-guide/delegates/index.md)
71+
- [Events](../../programming-guide/events/index.md)
72+
- [Checked and unchecked](../keywords/checked-and-unchecked.md)
73+
- [Arithmetic operators](arithmetic-operators.md)
74+
- [+ and += operators](addition-operator.md)

docs/csharp/language-reference/operators/toc.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
- name: + and += operators
3030
href: addition-operator.md
3131
displayName: string concatenation, delegate combination, event subscription
32-
- name: "- operator"
32+
- name: "- and -= operators"
3333
href: subtraction-operator.md
34+
displayName: delegate removal, event unsubscription
3435
- name: = operator
3536
href: assignment-operator.md
3637
- name: "?: operator"
3738
href: conditional-operator.md
38-
- name: -= operator
39-
href: subtraction-assignment-operator.md
4039
- name: "?? operator"
4140
href: null-coalescing-operator.md
4241
- name: => operator

docs/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ You subscribe to an event that is published by another class when you want to wr
9898
- [Events](../../../csharp/programming-guide/events/index.md)
9999
- [event](../../../csharp/language-reference/keywords/event.md)
100100
- [How to: Publish Events that Conform to .NET Framework Guidelines](../../../csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines.md)
101-
- [-= Operator (C# Reference)](../../language-reference/operators/subtraction-assignment-operator.md)
101+
- [- and -= operators](../../language-reference/operators/subtraction-operator.md)
102102
- [+ and += operators](../../language-reference/operators/addition-operator.md)

docs/framework/winforms/controls/how-to-add-to-or-remove-from-a-collection-of-controls-at-run-time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Common tasks in application development are adding controls to and removing cont
6060

6161
### To remove controls from a collection programmatically
6262

63-
1. Remove the event handler from the event. In Visual Basic, use the [RemoveHandler Statement](~/docs/visual-basic/language-reference/statements/removehandler-statement.md) keyword; in Visual C#, use the [-= Operator (C# Reference)](~/docs/csharp/language-reference/operators/subtraction-assignment-operator.md).
63+
1. Remove the event handler from the event. In Visual Basic, use the [RemoveHandler Statement](~/docs/visual-basic/language-reference/statements/removehandler-statement.md) keyword; in C#, use the [-= operator](~/docs/csharp/language-reference/operators/subtraction-operator.md).
6464

6565
2. Use the `Remove` method to delete the desired control from the panel's `Controls` collection.
6666

0 commit comments

Comments
 (0)