|
1 | 1 | --- |
2 | | -title: "- operator - C# Reference" |
| 2 | +title: "- and -= operators - C# Reference" |
3 | 3 | ms.custom: seodec18 |
4 | | - |
5 | | -ms.date: 07/20/2015 |
| 4 | +ms.date: 05/27/2019 |
6 | 5 | f1_keywords: |
7 | 6 | - "-_CSharpKeyword" |
| 7 | + - "-=_CSharpKeyword" |
8 | 8 | helpviewer_keywords: |
| 9 | + - "subtraction operator [C#]" |
| 10 | + - "delegate removal [C#]" |
9 | 11 | - "- operator [C#]" |
10 | | - - "subtraction operator (-) [C#]" |
| 12 | + - "subtraction assignment operator [C#]" |
| 13 | + - "event unsubscription [C#]" |
| 14 | + - "-= operator [C#]" |
11 | 15 | ms.assetid: 4de7a4fa-c69d-48e6-aff1-3130af970b2d |
12 | 16 | --- |
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 |
14 | 44 |
|
15 | | -The `-` operator can function as either a unary or a binary operator. |
| 45 | +```csharp |
| 46 | +x = x - y |
| 47 | +``` |
16 | 48 |
|
17 | | -## Remarks |
| 49 | +except that `x` is only evaluated once. |
| 50 | + |
| 51 | +The following example demonstrates the usage of the `-=` operator: |
18 | 52 |
|
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)] |
20 | 54 |
|
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). |
22 | 56 |
|
23 | | -Delegate types also provide a binary `-` operator, which performs delegate removal. |
| 57 | +## Operator overloadability |
24 | 58 |
|
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. |
26 | 60 |
|
27 | | -## Example |
| 61 | +## C# language specification |
28 | 62 |
|
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). |
30 | 64 |
|
31 | 65 | ## See also |
32 | 66 |
|
33 | 67 | - [C# Reference](../index.md) |
34 | 68 | - [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) |
0 commit comments