Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
+
and +=
The +
and +=
operators are supported by the built-in integral and floating-point numeric types, the string type, and delegate types.
For information about the arithmetic +
operator, see the Unary plus and minus operators and Addition operator + sections of the Arithmetic operators article.
When one or both operands are of type string, the +
operator concatenates the string representations of its operands (the string representation of null
is an empty string):
Console.WriteLine("Forgot" + "white space");
Console.WriteLine("Probably the oldest constant: " + Math.PI);
Console.WriteLine(null + "Nothing to add.");
// Output:
// Forgotwhite space
// Probably the oldest constant: 3.14159265358979
// Nothing to add.
String interpolation provides a more convenient way to format strings:
Console.WriteLine($"Probably the oldest constant: {Math.PI:F2}");
// Output:
// Probably the oldest constant: 3.14
You can use string interpolation to initialize a constant string when all the expressions used for placeholders are also constant strings.
Beginning with C# 11, the +
operator performs string concatenation for UTF-8 literal strings. This operator concatenates two ReadOnlySpan<byte>
objects.
For operands of the same delegate type, the +
operator returns a new delegate instance that, when invoked, invokes the left-hand operand and then invokes the right-hand 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:
Action a = () => Console.Write("a");
Action b = () => Console.Write("b");
Action ab = a + b;
ab(); // output: ab
To perform delegate removal, use the -
operator.
For more information about delegate types, see Delegates.
An expression using the +=
operator, such as
x += y
Is equivalent to:
x = x + y
Except that x
is only evaluated once.
The following example demonstrates the usage of the +=
operator:
int i = 5;
i += 9;
Console.WriteLine(i);
// Output: 14
string story = "Start. ";
story += "End.";
Console.WriteLine(story);
// Output: Start. End.
Action printer = () => Console.Write("a");
printer(); // output: a
Console.WriteLine();
printer += () => Console.Write("b");
printer(); // output: ab
You also use the +=
operator to specify an event handler method when you subscribe to an event. For more information, see How to: subscribe to and unsubscribe from events.
A user-defined type can overload the +
operator. When a binary +
operator is overloaded, the +=
operator is also implicitly overloaded. A user-defined type can't explicitly overload the +=
operator.
For more information, see the Unary plus operator and Addition operator sections of the C# language specification.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now