Manipulating Strings in C#—Concatenating Strings
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
Concatenating Strings
You can concatenate strings and produce a new string from the combination. There are two ways to combine strings. One way is using a method called Concat in the string class. The other is to use the + or the += operator. The compiler then does the work of changing + and += to calls to the Concat function. In essence, there is one function to concatenate, and two ways of calling it.
To build one string from the combination of two or more strings:
- 1. Declare a string variable to hold the result of the concatenation.
- 2. Type
= System.String.Concat(piece1,piece2);wherepiece1andpiece2are either string literals or string variables (Figure 4.20).
- or
- Type
= piece1 + piece2;wherepiece1andpiece2are either string literals or string variables (Figure 4.21).

- Figure 4.21.
Concatis a method inSystem.Stringavailable to all languages, but the C# language also provides a quick way to call theConcatfunction: using the plus sign.
- or
- Instead of declaring a new variable to hold the result you can use an existing variable and just append to it by typing
piece1 += piece2, wherepiece1is the variable that contains the original string andpiece2is the variable you wish to append to the end of piece1 (Figure 4.22).

- Figure 4.22. If you're used to programming in VBScript whenever you want to take an existing variable and append to it, you end up writing an expression like
var1 = var1 + newvar. In C# the same expression can be compacted intovar1 += newvar.
Tips
- The
Concatfunction is a static function. For now it's enough to know that to invoke a static function you don't need to create an instance of the class. You simply use the name of the class plus a dot plus the name of the function, as in the examples above.
- The
- There are nine versions of the
Concatfunction. You can pass to theConcatfunction as many segments as you like. It's also possible to pass objects to the function that aren't strings. TheConcatfunction will turn the objects into strings by calling theToStringfunction on each object. See "Representing Objects as Strings" later in this chapter (Figure 4.23).
- There are nine versions of the

- Figure 4.23. The parameters for the
Concatfunction are of typeObjectwhich means that you can pass in strings, numbers, instances of classes, etc.
- When you use the plus operator or the plus equal operator, the compiler turns the code into calls to the
Concatfunction. The compiler is smart enough to know that if you glue three pieces together with a plus sign, it should use the version of theConcatfunction that accepts three parameters, instead of concatenating the first to the second and then concatenating the result to the third, which would be inefficient.
- When you use the plus operator or the plus equal operator, the compiler turns the code into calls to the
- Remember that string buffers can't be changed, so when you concatenate to a string using any of the above mechanisms, the result is that the .NET Framework allocates a new string object in memory to store the characters from all the pieces in the string.
|


