Bird
Raised Fist0
C Sharp (C#)programming~10 mins

String concatenation behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - String concatenation behavior
Start with strings
Apply + operator
Create new string by joining
Assign or use result
End
This flow shows how C# joins strings using the + operator by creating a new combined string.
Execution Sample
C Sharp (C#)
string a = "Hello";
string b = "World";
string c = a + ", " + b + "!";
Console.WriteLine(c);
This code joins three strings with + and prints the combined result.
Execution Table
StepOperationOperandsResultNotes
1Assign a"Hello"a = "Hello"Variable a set to Hello
2Assign b"World"b = "World"Variable b set to World
3Concatenate a + ", ""Hello" + ", ""Hello, "First join creates Hello,
4Concatenate previous + b"Hello, " + "World""Hello, World"Second join adds World
5Concatenate previous + "!""Hello, World" + "!""Hello, World!"Final join adds exclamation
6Assign cResult of step 5c = "Hello, World!"Variable c set to full string
7Print ccOutput: Hello, World!Console prints the combined string
💡 All concatenations complete, final string assigned and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
anull"Hello""Hello""Hello""Hello"
bnullnull"World""World""World"
cnullnullnull"Hello, World!""Hello, World!"
Key Moments - 2 Insights
Why does each + operation create a new string instead of changing the original?
In C#, strings are immutable. Each + creates a new string object, shown in steps 3, 4, and 5 where new results form without altering a or b.
What happens if we print a or b after concatenation?
Variables a and b keep their original values as shown in variable_tracker after steps 1 and 2, because concatenation does not modify them.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of c after step 6?
A"Hello, World!"
B"Hello"
C"World"
D"Hello, "
💡 Hint
Check the Result column at step 6 in execution_table.
At which step does the first concatenation happen?
AStep 1
BStep 3
CStep 5
DStep 7
💡 Hint
Look for the first + operation in the Operation column.
If we changed b to "Everyone", what would be the output at step 7?
A"Hello, Everyone"
B"Hello, World!"
C"Hello, Everyone!"
D"Hello World!"
💡 Hint
Consider how b is used in concatenation steps 4 and 5.
Concept Snapshot
String concatenation in C# uses + operator.
Each + creates a new string (strings are immutable).
Original strings stay unchanged.
Use + to join multiple strings.
Result can be assigned or printed.
Full Transcript
This visual trace shows how C# joins strings using the + operator. First, variables a and b get assigned "Hello" and "World". Then, step by step, the program joins a with a comma and space, then adds b, then adds an exclamation mark. Each + creates a new string without changing the originals. Finally, the combined string is assigned to c and printed. The variable tracker confirms a and b keep their original values. This helps beginners see that string concatenation builds new strings rather than modifying existing ones.

Practice

(1/5)
1. What does the + operator do when used between two strings in C#?
easy
A. It multiplies the two strings.
B. It subtracts the second string from the first.
C. It joins the two strings into one longer string.
D. It compares the two strings for equality.

Solution

  1. Step 1: Understand the + operator with strings

    In C#, the + operator combines two strings by joining them end to end.
  2. Step 2: Check other options

    Subtracting, multiplying, or comparing strings are not done with +. Those operations use other operators or methods.
  3. Final Answer:

    It joins the two strings into one longer string. -> Option C
  4. Quick Check:

    String + String = Joined String [OK]
Hint: Remember: + joins strings like glue [OK]
Common Mistakes:
  • Thinking + subtracts or multiplies strings
  • Confusing + with comparison operators
  • Assuming + works only with numbers
2. Which of the following is the correct syntax to concatenate two strings str1 and str2 in C#?
easy
A. string result = str1 * str2;
B. string result = str1 + str2;
C. string result = str1 - str2;
D. string result = str1 / str2;

Solution

  1. Step 1: Identify valid operators for string concatenation

    The + operator is used to join strings in C#. Other arithmetic operators like -, *, / are invalid for strings.
  2. Step 2: Confirm syntax correctness

    The statement string result = str1 + str2; correctly concatenates and assigns the result.
  3. Final Answer:

    string result = str1 + str2; -> Option B
  4. Quick Check:

    Use + for string join [OK]
Hint: Use + to join strings, not arithmetic signs [OK]
Common Mistakes:
  • Using - or * instead of + for strings
  • Missing semicolon at end
  • Trying to divide or multiply strings
3. What is the output of this C# code?
string a = "Hello";
string b = "World";
string c = a + ", " + b + "!";
Console.WriteLine(c);
medium
A. Hello, World!
B. Hello World!
C. Hello,World!
D. HelloWorld!

Solution

  1. Step 1: Analyze string concatenation parts

    The code joins "Hello", ", ", "World", and "!" in order, so the result is "Hello, World!".
  2. Step 2: Confirm output of Console.WriteLine

    Console.WriteLine prints the combined string exactly as concatenated.
  3. Final Answer:

    Hello, World! -> Option A
  4. Quick Check:

    Strings + punctuation join as typed [OK]
Hint: Watch spaces and punctuation in concatenation [OK]
Common Mistakes:
  • Missing spaces or commas in output
  • Ignoring punctuation strings
  • Confusing concatenation order
4. Identify the error in this code snippet:
string first = "Good";
string second = "Morning";
string message = first + second;
message += 5;
Console.WriteLine(message);
medium
A. Missing space between first and second strings.
B. Cannot add integer 5 to a string using += operator.
C. Variable 'message' is not declared.
D. Console.WriteLine syntax is incorrect.

Solution

  1. Step 1: Check string concatenation and += usage

    The code concatenates "Good" and "Morning" without space, so message becomes "GoodMorning". Adding 5 converts 5 to string "5" and appends it, resulting in "GoodMorning5".
  2. Step 2: Identify the main issue

    The code runs without error, but the missing space between words is a logical mistake causing output to look wrong.
  3. Final Answer:

    Missing space between first and second strings. -> Option A
  4. Quick Check:

    Check spaces when joining strings [OK]
Hint: Add spaces explicitly when joining words [OK]
Common Mistakes:
  • Assuming += 5 causes error
  • Ignoring missing spaces in output
  • Thinking Console.WriteLine is wrong
5. Given a list of words string[] words = {"apple", "banana", "cherry"};, which code correctly concatenates them into a single comma-separated string using string.Concat?
hard
A. string result = string.Concat(words + ",");
B. string result = string.Concat(words[0] + words[1] + words[2]);
C. string result = string.Concat(words);
D. string result = string.Concat(words[0], ",", words[1], ",", words[2]);

Solution

  1. Step 1: Understand string.Concat usage

    string.Concat can join multiple strings passed as arguments. To add commas, they must be separate arguments.
  2. Step 2: Analyze each option

    string result = string.Concat(words[0], ",", words[1], ",", words[2]); passes each word and commas separately, correctly joining as "apple,banana,cherry". string result = string.Concat(words + ","); tries to add comma to array, invalid. string result = string.Concat(words); joins words without commas. string result = string.Concat(words[0] + words[1] + words[2]); concatenates words without commas inside Concat.
  3. Final Answer:

    string result = string.Concat(words[0], ",", words[1], ",", words[2]); -> Option D
  4. Quick Check:

    Pass each string and separator separately to Concat [OK]
Hint: Pass each string and comma separately to string.Concat [OK]
Common Mistakes:
  • Trying to add commas inside array
  • Passing array directly to Concat without separators
  • Using + inside Concat argument incorrectly