Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 47 additions & 24 deletions docs/csharp/language-reference/operators/addition-operator.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "+ Operator (C# Reference)"
ms.date: 07/20/2015
ms.date: 10/22/2018
f1_keywords:
- "+_CSharpKeyword"
helpviewer_keywords:
Expand All @@ -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)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 <xref:System.Math.IEEERemainder%2A?displayProperty=nameWithType> method.
Expand Down