#62 – String Concatenation

In C#, you can use the ‘+’ (plus) character to concatenate multiple strings together.

 string s1 = "C#";
 string s2 = "fun";
 string s3 = s1 + " is " + s2;   // "C# is fun"

You can use one or more concatenation operators in the same expression and you can use an expression containing the concatenation operator anywhere that a string value is expected.

You can also use String.Concat or String.Format to concatenate strings.

 // Concat method
 string s4 = String.Concat(new object[] { "The ", 3, " musketeers" });
 string s5 = String.Concat("This", "That");    // ThisThat

 // Use String.Format to concatenate
 string s6 = string.Format("{0}{1}{2}", s1, " is ", s2);
Advertisements