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 insideConsole.WriteLine, the variable itself stays unchanged. To keep the result, assign it back withname = name + "!";orname += "!";.
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";
- Start with the fixed text. The sentence begins
"My name is ", with a space before the closing quote because the name follows it directly. - Add the variable. Put
nameafter a+, outside any quotes. Inside quotes it would be literal text, so it would print the word name instead of John Doe. - Add the middle text.
" and I code in "needs spaces on both sides. - 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 + 3is"123". - Strings are immutable: concatenation produces a new string.
$"..."interpolation is a tidier alternative for longer messages.