diff --git a/docs/csharp/language-reference/operators/addition-assignment-operator.md b/docs/csharp/language-reference/operators/addition-assignment-operator.md index bfd37139c13ed..fe2963a982d28 100644 --- a/docs/csharp/language-reference/operators/addition-assignment-operator.md +++ b/docs/csharp/language-reference/operators/addition-assignment-operator.md @@ -1,40 +1,46 @@ --- title: "+= Operator (C# Reference)" -ms.date: 07/20/2015 +ms.date: 10/22/2018 f1_keywords: - "+=_CSharpKeyword" helpviewer_keywords: - "+= operator [C#]" - "addition assignment operator (+=) [C#]" + - "event subscription [C#]" ms.assetid: 9cdf97e6-331d-492b-85e1-3ec3171484e9 --- # += Operator (C# Reference) -The addition assignment operator. - -## Remarks - An expression using the `+=` assignment operator, such as - -```csharp -x += y + +The addition assignment operator. + +An expression using the `+=` operator, such as + +```csharp +x += y ``` - - is equivalent to - -```csharp -x = x + y + +is equivalent to + +```csharp +x = x + y ``` + +except that `x` is only evaluated once. - except that `x` is only evaluated once. The meaning of the [+ operator](../../../csharp/language-reference/operators/addition-operator.md) depends on the types of `x` and `y` (addition for numeric operands, concatenation for string operands, and so forth). - - The `+=` operator cannot be overloaded directly, but user-defined types can overload the [+ operator](../../../csharp/language-reference/operators/addition-operator.md) (see [operator](../../../csharp/language-reference/keywords/operator.md)). - - The `+=` operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. The use of the `+=` operator in this context is referred to as *subscribing to an event*. For more information, see [How to: Subscribe to and Unsubscribe from Events](../../../csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events.md) and [Delegates](../../../csharp/programming-guide/delegates/index.md). - -## Example - [!code-csharp[csRefOperators#35](../../../csharp/language-reference/operators/codesnippet/CSharp/addition-assignment-operator_1.cs)] +For numeric types, the [addition operator](addition-operator.md) `+` computes the sum of its operands. If one or both operands is of type [string](../keywords/string.md), it concatenates the string representations of its operands. For delegate types, the `+` operator returns a new delegate instance that is combination of its operands. + +If a user-defined type [overloads](../keywords/operator.md) the [addition operator](addition-operator.md) `+`, the addition assignment operator `+=` is implicitly overloaded. + +You also use the `+=` operator to specify an event handler method when you subscribe to 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). + +The following example demonstrates the usage of the `+=` operator: + +[!code-csharp-interactive[+= examples](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddAndAssign)] -## See Also +## See also -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [C# Operators](../../../csharp/language-reference/operators/index.md) +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Operators](index.md) +- [Events](../../programming-guide/events/index.md) +- [Delegates](../../programming-guide/delegates/index.md) diff --git a/docs/csharp/language-reference/operators/addition-operator.md b/docs/csharp/language-reference/operators/addition-operator.md index d4b5a6a034c2f..8ef9c58abf5cb 100644 --- a/docs/csharp/language-reference/operators/addition-operator.md +++ b/docs/csharp/language-reference/operators/addition-operator.md @@ -1,6 +1,6 @@ --- title: "+ Operator (C# Reference)" -ms.date: 07/20/2015 +ms.date: 10/22/2018 f1_keywords: - "+_CSharpKeyword" helpviewer_keywords: @@ -10,26 +10,49 @@ helpviewer_keywords: ms.assetid: 93e56486-bb42-43c1-bd43-60af11e64e67 --- # + Operator (C# Reference) -The `+` operator can function as either a unary or a binary operator. - -## Remarks - Unary `+` operators are predefined for all numeric types. The result of a unary `+` operation on a numeric type is just the value of the operand. - - Binary `+` operators are predefined for numeric and string types. For numeric types, + computes the sum of its two operands. When one or both operands are of type string, + concatenates the string representations of the operands. - - Delegate types also provide a binary `+` operator, which performs delegate concatenation. - - User-defined types can overload the unary `+` and binary `+` operators. Operations on integral types are generally allowed on enumeration. For more information, see [operator (C# Reference)](../../../csharp/language-reference/keywords/operator.md). - -## Example - [!code-csharp[csRefOperators#28](../../../csharp/language-reference/operators/codesnippet/CSharp/addition-operator_1.cs)] - -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See Also - -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [C# Operators](../../../csharp/language-reference/operators/index.md) -- [operator (C# Reference)](../../../csharp/language-reference/keywords/operator.md) + +The `+` operator is supported in two forms: a unary plus operator or a binary addition operator. + +User-defined types can [overload](../keywords/operator.md) the unary and binary `+` operators. When a binary `+` operator is overloaded, the [addition assignment operator](addition-assignment-operator.md) `+=` is also implicitly overloaded. + +## Unary plus operator + +The unary `+` operator returns the value of its operand. It's supported by all numeric types. + +## Numeric addition + +For numeric types, the `+` operator computes the sum of its operands: + +[!code-csharp-interactive[numeric addition](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddNumerics)] + +## String concatenation + +When one or both operands are of type [string](../keywords/string.md), the `+` operator concatenates the string representations of its operands: + +[!code-csharp-interactive[string concatenation](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddStrings)] + +Starting with C# 6, [string interpolation](../tokens/interpolated.md) provides a more convenient way to format strings: + +[!code-csharp-interactive[string interpolation](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#UseStringInterpolation)] + +## Delegate combination + +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: + +[!code-csharp-interactive[delegate combination](~/samples/snippets/csharp/language-reference/operators/AdditionExamples.cs#AddDelegates)] + +For more information about delegate types, see [Delegates](../../programming-guide/delegates/index.md). + +## C# language specification + +For more information, see the [Unary plus operator](~/_csharplang/spec/expressions.md#unary-plus-operator) and [Addition operator](~/_csharplang/spec/expressions.md#addition-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) +- [String interpolation](../tokens/interpolated.md) +- [How to: Concatenate Multiple Strings](../../how-to/concatenate-multiple-strings.md) +- [Delegates](../../programming-guide/delegates/index.md) +- [Checked and unchecked](../keywords/checked-and-unchecked.md) diff --git a/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-assignment-operator_1.cs b/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-assignment-operator_1.cs deleted file mode 100644 index 382b6a4ae1d3c..0000000000000 --- a/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-assignment-operator_1.cs +++ /dev/null @@ -1,20 +0,0 @@ - class AddAssigment - { - static void Main() - { - //addition - int a = 5; - a += 6; - Console.WriteLine(a); - - //string concatenation - string s = "Hello"; - s += " world"; - Console.WriteLine(s); - } - } - /* - Output: - 11 - Hello world - */ diff --git a/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-operator_1.cs b/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-operator_1.cs deleted file mode 100644 index 1afe45cb1139e..0000000000000 --- a/docs/csharp/language-reference/operators/codesnippet/CSharp/addition-operator_1.cs +++ /dev/null @@ -1,21 +0,0 @@ - - class Plus - { - static void Main() - { - Console.WriteLine(+5); // unary plus - Console.WriteLine(5 + 5); // addition - Console.WriteLine(5 + .5); // addition - Console.WriteLine("5" + "5"); // string concatenation - Console.WriteLine(5.0 + "5"); // string concatenation - // note automatic conversion from double to string - } - } - /* - Output: - 5 - 10 - 5.5 - 55 - 55 - */ \ No newline at end of file diff --git a/docs/csharp/language-reference/operators/remainder-operator.md b/docs/csharp/language-reference/operators/remainder-operator.md index 777c9916ccb02..dddb6783de430 100644 --- a/docs/csharp/language-reference/operators/remainder-operator.md +++ b/docs/csharp/language-reference/operators/remainder-operator.md @@ -10,7 +10,9 @@ ms.assetid: 3b74f4f9-fd9c-45e7-84fa-c8d71a0dfad7 --- # % Operator (C# Reference) -The remainder operator `%` computes the remainder after dividing its first operand by its second operand. User-defined types can [overload](../keywords/operator.md) the `%` operator. When the `%` is overloaded, the [remainder assignment operator](remainder-assignment-operator.md) `%=` is also implicitly overloaded. +The remainder operator `%` computes the remainder after dividing its first operand by its second operand. + +User-defined types can [overload](../keywords/operator.md) the `%` operator. When the `%` is overloaded, the [remainder assignment operator](remainder-assignment-operator.md) `%=` is also implicitly overloaded. All numeric types support the remainder operator. @@ -27,7 +29,7 @@ For the [float](../keywords/float.md) and [double](../keywords/double.md) operan - the sign of `z`, if non-zero, is the same as the sign of `x`; - the absolute value of `z` is the value produced by `|x| - n * |y|` where `n` is the largest possible integer that is less than or equal to `|x| / |y|` and `|x|` and `|y|` are the absolute values of `x` and `y`, respectively. -For information about behavior of the `%` operator in case of non-finite operands, see the [Remainder operator](~/_csharplang/spec/expressions.md#remainder-operator) section of the [C# language specification](/dotnet/csharp/language-reference/language-specification/index). +For information about the behavior of the `%` operator with non-finite operands, see the [Remainder operator](~/_csharplang/spec/expressions.md#remainder-operator) section of the [C# language specification](../language-specification/index.md). > [!NOTE] > This method of computing the remainder is analogous to that used for integer operands, but differs from the IEEE 754. If you need the remainder operation that complies with the IEEE 754, use the method.