Start learning free

LESSON 8 OF 347 · WELCOME TO C#

String Concatenation in C#: The + Operator and Interpolation

Lesson Read the full explanation

We can concatenate strings with the plus operator (+).

Here is a program that concatenates the strings "I love coding. " and "It is fun!" and assigns the resulted string to a variable message:

string message = "I love coding. " + "It is fun!";
Console.WriteLine(message);

Output:

I love coding. It is fun!

Concatenation with variables:

String concatenation is redundat when used with string literals because we already know what the resulted string would be.

For example, in our program, we could have initialized message directly with the bigger string instead of doing concatenation:

string message = "I love coding. It is fun!";
Console.WriteLine(message);

String concatenation is used to dynamically generate messages that can be different depending on context.

For example, I might want to print a specific message about my favorite animal, whatever that is:

string favoriteAnimal = "parrot";

string message = "My favorite animal is the " + favoriteAnimal;

Console.WriteLine(message);

Output:

My favorite animal is the parrot

The value of message will differ if my favorite animal was "dog" or "cat", but it would still make sense.


Concatenating multiple strings:

We can concatenate as many strings as we want in the same line of code by adding a + before each string:

string animal = "parrot";
string name = "Andy";

Console.WriteLine(name + ", that's a nice " + animal + " you got there!");

Output:

Andy, that's a nice parrot you got there!

Be careful to add whitespaces and other characters you want in your message


Assignment
Follow the Coding Tutorial and let's practice with string concatenation!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    String Concatenation in C#, explained

    What concatenation is

    Concatenation means joining strings end to end to make a longer one. In C# you do it with the + operator:

    string message = "I love coding. " + "It is fun!";
    Console.WriteLine(message);
    
    I love coding. It is fun!
    

    Joining two literals is pointless, since you could type the longer string directly. Concatenation earns its keep when some of the pieces are variables, so the final text depends on the data:

    string favoriteAnimal = "parrot";
    string message = "My favorite animal is the " + favoriteAnimal;
    Console.WriteLine(message);   // My favorite animal is the parrot
    

    Change favoriteAnimal to "dog" and the sentence still makes sense, without touching the line that builds it.

    Joining many pieces

    You can chain as many + operators as you like. The compiler evaluates them from left to right:

    string animal = "parrot";
    string name = "Andy";
    
    Console.WriteLine(name + ", that's a nice " + animal + " you got there!");
    
    Andy, that's a nice parrot you got there!
    

    C# adds nothing between the pieces. Every space and comma has to be written into one of the strings, which is why ", that's a nice " has a space at each end. Forgetting one gives output like Andythat's.

    Mixing strings and other types

    If one side of + is a string, C# converts the other side to text and joins them:

    int age = 32;
    Console.WriteLine("I am " + age + " years old");   // I am 32 years old
    

    That conversion creates a trap when numbers and text sit in the same chain, because the order of evaluation decides whether + adds or joins:

    Console.WriteLine(1 + 2 + "3");     // 33
    Console.WriteLine("1" + 2 + 3);     // 123
    Console.WriteLine("Total: " + (1 + 2));   // Total: 3
    

    In the first line, 1 + 2 comes first and is added to 3; the result is then joined with "3". In the second, the string is on the left from the start, so every + joins. Parentheses make the arithmetic happen first, as in the third line.

    Interpolated strings

    Long chains of + are easy to get wrong. C# also offers interpolated strings: put $ before the opening quote and write variables in braces.

    string animal = "parrot";
    string name = "Andy";
    Console.WriteLine($"{name}, that's a nice {animal} you got there!");
    

    This prints the same sentence as the chain above. The lesson uses +, and you should know it well, but the $ form is common in everyday C#.

    Strings don't change in place

    Strings in C# are immutable. Concatenation never modifies an existing string; it builds a new one:

    string a = "Hello";
    string b = a + " world";
    a += "!";
    Console.WriteLine(a + " " + b);   // Hello! Hello world
    

    Building b leaves a as "Hello". Only the assignment a += "!" gives a a new value. A null string joins as an empty string, so "[" + s + "]" with a null s prints [] instead of failing.

    Common mistakes

    • Missing spaces. The output looks glued together because no space was written into any of the pieces. Check both ends of every quoted fragment.
    • Quoting a variable. Writing "name" prints the word, not the value stored in the variable.
    • Forgetting a +. Two pieces side by side, such as "Hi " name, are a syntax error.
    • Expecting + to change a variable. name + "!" alone on a line does not compile, because a bare expression is not a statement. Even in places where it is allowed, such as inside Console.WriteLine, the variable itself stays unchanged. To keep the result, assign it back with name = name + "!"; or name += "!";.

    The exercise, step by step

    The starter code declares two variables, and you add one Console.WriteLine call:

    string name = "John Doe";
    string language = "Python";
    
    1. Start with the fixed text. The sentence begins "My name is ", with a space before the closing quote because the name follows it directly.
    2. Add the variable. Put name after a +, outside any quotes. Inside quotes it would be literal text, so it would print the word name instead of John Doe.
    3. Add the middle text. " and I code in " needs spaces on both sides.
    4. Finish with language. Again no quotes.
    string name = "John Doe";
    string language = "Python";
    
    Console.WriteLine("My name is " + name + " and I code in " + language);
    

    It prints My name is John Doe and I code in Python. Change either variable and the sentence follows.

    Key takeaways

    • + between strings joins them, and nothing is inserted automatically, so spaces are your job.
    • Variables go outside the quotes; text inside the quotes is printed literally.
    • Evaluation is left to right, so 1 + 2 + "3" is "33", while "1" + 2 + 3 is "123".
    • Strings are immutable: concatenation produces a new string.
    • $"..." interpolation is a tidier alternative for longer messages.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free