-
Couldn't load subscription status.
- Fork 6.1k
Revised and merged the subtraction operator articles #12541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BillWagner
merged 2 commits into
dotnet:master
from
pkulikov:merge-and-revise-subtraction-operators
May 28, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
docs/csharp/language-reference/operators/subtraction-assignment-operator.md
This file was deleted.
Oops, something went wrong.
67 changes: 53 additions & 14 deletions
67
docs/csharp/language-reference/operators/subtraction-operator.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,74 @@ | ||
| --- | ||
| title: "- operator - C# Reference" | ||
| title: "- and -= operators - C# Reference" | ||
| ms.custom: seodec18 | ||
|
|
||
| ms.date: 07/20/2015 | ||
| ms.date: 05/27/2019 | ||
| f1_keywords: | ||
| - "-_CSharpKeyword" | ||
| - "-=_CSharpKeyword" | ||
| helpviewer_keywords: | ||
| - "subtraction operator [C#]" | ||
| - "delegate removal [C#]" | ||
| - "- operator [C#]" | ||
| - "subtraction operator (-) [C#]" | ||
| - "subtraction assignment operator [C#]" | ||
| - "event unsubscription [C#]" | ||
| - "-= operator [C#]" | ||
| ms.assetid: 4de7a4fa-c69d-48e6-aff1-3130af970b2d | ||
| --- | ||
| # - operator (C# Reference) | ||
| # - and -= operators (C# Reference) | ||
|
|
||
| The `-` operator is supported by the built-in numeric types and [delegate](../keywords/delegate.md) types. | ||
|
|
||
| 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. | ||
|
|
||
| ## Delegate removal | ||
|
|
||
| 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. | ||
| - 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](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#DelegateRemoval)] | ||
|
|
||
| ## Subtraction assignment operator -= | ||
|
|
||
| An expression using the `-=` operator, such as | ||
|
|
||
| ```csharp | ||
| x -= y | ||
| ``` | ||
|
|
||
| is equivalent to | ||
|
|
||
| The `-` operator can function as either a unary or a binary operator. | ||
| ```csharp | ||
| x = x - y | ||
| ``` | ||
|
|
||
| ## Remarks | ||
| except that `x` is only evaluated once. | ||
|
|
||
| The following example demonstrates the usage of the `-=` operator: | ||
|
|
||
| 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. | ||
| [!code-csharp-interactive[-= examples](~/samples/csharp/language-reference/operators/SubtractionOperator.cs#SubtractAndAssign)] | ||
|
|
||
| Binary `-` operators are predefined for all numeric and enumeration types to subtract the second operand from the first. | ||
| 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). | ||
|
|
||
| Delegate types also provide a binary `-` operator, which performs delegate removal. | ||
| ## Operator overloadability | ||
|
|
||
| User-defined types can overload the unary `-` and binary `-` operators. For more information, see [operator keyword](../keywords/operator.md). | ||
| 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. | ||
|
|
||
| ## Example | ||
| ## C# language specification | ||
|
|
||
| [!code-csharp[csRefOperators#40](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefOperators/CS/csrefOperators.cs#40)] | ||
| 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). | ||
|
|
||
| ## See also | ||
|
|
||
| - [C# Reference](../index.md) | ||
| - [C# Programming Guide](../../programming-guide/index.md) | ||
| - [C# operators](index.md) | ||
| - [C# Operators](index.md) | ||
| - [Delegates](../../programming-guide/delegates/index.md) | ||
| - [Events](../../programming-guide/events/index.md) | ||
| - [Checked and unchecked](../keywords/checked-and-unchecked.md) | ||
| - [Arithmetic operators](arithmetic-operators.md) | ||
| - [+ and += operators](addition-operator.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to my comments on dotnet/samples#929 This is the condition that often surprises developers. An example would help.