+ 及 += 運算子 (C# 參考)+ and += operators (C# reference)

+ 運算子支援內建數值型別、string 型別和 delegate 型別。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

當其中一或兩個運算元的型別為 string 時,+ 運算子會串連其運算元的字串表示: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