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):
C#
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.
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.
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. 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:
C#
Action a = () => Console.Write("a");
Action b = () => Console.Write("b");
Action ab = a + b;
ab(); // output: ab
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.
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.
Povratne informacije o proizvodu .NET
.NET je projekt otvorenog koda. Odaberite vezu za slanje povratnih informacija:
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.