C# String Append: Add Strings Together

Dot Net Perls
String type

You want to append one string to another string in a C# program. There is no actual "Append" method on the string type, so you need a quick reference for how to add one string on the end to an existing string. You can add one string to another string, effectively appending strings in the C# language, by using an overloaded operator += or +.

Example 1

Many developers are familiar with appending strings, but the string type doesn't have an "Append" method. Instead, it is best to use the plus operator (+) on different instances of strings. The next example shows how you can append string references and literals, and also the output of the console program.

Program that appends strings [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// This is your input string.
	//
	string value = "Dot Net ";
	//
	// Append the word "Perls" to the string.
	//
	value += "Perls";
	//
	// Write the string to the screen.
	//
	Console.WriteLine(value);
	//
	// Append this word to the string.
	//
	value += " Basket";
	//
	// Write the new string.
	//
	Console.WriteLine(value);
    }
}

Output

Dot Net Perls
Dot Net Perls Basket

Description. The code defines the Main entry point. Inside Main, a string reference is assigned the literal value "Dot Net ". Next, the word "Perls" is actually appended to the string with the += operator. This operator doesn't add integers, but actually combines the two strings. Finally, another word is appended to the same string variable.

Example 2

Let's look at how you can append string values, and also append multiple values at once. This example creates a two-line string, using three string variables. We also see the Environment.NewLine property, which you can use directly in any program that includes the System namespace.

Program that appends line to string [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// Your input string.
	//
	string value1 = "One";
	//
	// Another input string.
	//
	string value2 = "Two";
	//
	// Append newline to string and also string.
	//
	value1 += Environment.NewLine +
	    value2;
	//
	// Write output to the console.
	//
	Console.WriteLine(value1);
    }
}

Output

One
Two

Note on Console.WriteLine method. This method writes the string to your console window. It only adds a newline at the end of the output string, so the newline in between "One" and "Two" is from the string append.

Console.WriteLine Use

Implementation

.NET Framework information

When you compile one of the above C# programs, the compiler will transform the + operators into calls to String.Concat. You can actually use String.Concat instead of the code here for the same effect. However, it is fine to think of this code as string appending, because the end result is the same, regardless of implementation.

String Concat Programs

Summary

We looked at how you can append strings in the C# language. It is easiest to use the + operator for quick programs. This content is useful for beginning developers or experienced developers who are changing languages.

String Type