8.4. String Concatenation Operators

As mentioned earlier, the + operator can be overloaded for different data types. In the case of C# strings, the + operator is overloaded to concatenate two strings. Listing 8.4 shows a simple concatenation example.

Listing 8.4. Using the + Operator on Strings (C#)
using System;

public class StringTest {

  public static void Main(string[] args) {
    string s = "Test";
    string t = " Case";
    int x = 10;
    Console.WriteLine(s+t);
    Console.WriteLine(s+10);
  }
}

The output of Listing 8.4 is as follows:

Test Case
Test10

Note that to use the + operator with a string as one operand and a numeric data type as the other operand, you convert the numeric data type to a string and then concatenate the resulting two strings.

Get .NET for Java Developers: Migrating to C# now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.