+ 和 += 运算符(C# 参考)+ and += operators (C# reference)

+ 运算符受内置数字类型、字符串类型和委托类型的支持。The + operator is supported by the built-in numeric types, 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.

字符串串联String concatenation

当其中的一个操作数是字符串类型或两个操作数都是字符串类型时,+ 运算符将其操作数的字符串表示形式串联在一起:When one or both operands are of type string, the + operator concatenates the string representations of its operands:

Console.WriteLine("Forgot" + "white space");
Console.WriteLine("Probably the oldest constant: " + Math.PI);
// Output:
// Forgotwhite space
// Probably the oldest constant: 3.14159265358979

从 C# 6 开始,字符串内插提供了格式化字符串的更便捷方式:Starting with C# 6, 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

委托组合Delegate combination

对于委托类型相同的操作数,+ 运算符在调用时返回新的委托实例,调用左侧的操作数,然后调用右侧的操作数。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. 如果任何操作数均为 null,则 + 运算符将返回另一个操作数(也可能是 null)的值。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.

加法赋值运算符 +=Addition assignment operator +=

使用 += 运算符的表达式,例如An expression using the += operator, such as

x += y

等效于is equivalent to

x = x + y

不同的是 x 只计算一次。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.

运算符可重载性Operator overloadability

用户定义的类型可以重载 + 运算符。A user-defined type can overload the + operator. 重载二元 + 运算符后,也会隐式重载 += 运算符。When a binary + operator is overloaded, the += operator is also implicitly overloaded. 用户定义类型不能显式重载 += 运算符。A user-defined type cannot explicitly overload the += operator.

C# 语言规范C# language specification

有关详细信息,请参阅C# 语言规范一元加运算符加法运算符部分。For more information, see the Unary plus operator and Addition operator sections of the C# language specification.

请参阅See also