String concatenation behavior in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we join strings together in C#, the way we do it affects how long the program takes to run.
We want to know how the time to combine strings grows as we add more pieces.
Analyze the time complexity of the following code snippet.
string result = "";
string[] words = new string[] {"a", "b", "c", "d", "e"};
foreach (string word in words)
{
result += word;
}
This code joins all words in the array into one string by adding each word to the result one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one string to another inside a loop.
- How many times: Once for each word in the array.
Each time we add a word, the program copies the whole current string and the new word to make a new string.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 copies (1+2+...+10) |
| 100 | About 5050 copies |
| 1000 | About 500,500 copies |
Pattern observation: The work grows much faster than the number of words because each addition copies a longer string.
Time Complexity: O(n²)
This means the time to join strings grows roughly with the square of the number of strings.
[X] Wrong: "Adding strings in a loop always takes time proportional to the number of strings (O(n))."
[OK] Correct: Each addition copies the whole current string, so the work adds up more than just once per string.
Understanding how string joining works helps you write faster code and explain your choices clearly in real projects.
"What if we used a StringBuilder instead of += inside the loop? How would the time complexity change?"
Practice
+ operator do when used between two strings in C#?Solution
Step 1: Understand the
In C#, the+operator with strings+operator combines two strings by joining them end to end.Step 2: Check other options
Subtracting, multiplying, or comparing strings are not done with+. Those operations use other operators or methods.Final Answer:
It joins the two strings into one longer string. -> Option CQuick Check:
String + String = Joined String [OK]
- Thinking + subtracts or multiplies strings
- Confusing + with comparison operators
- Assuming + works only with numbers
str1 and str2 in C#?Solution
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.Step 2: Confirm syntax correctness
The statementstring result = str1 + str2;correctly concatenates and assigns the result.Final Answer:
string result = str1 + str2; -> Option BQuick Check:
Use + for string join [OK]
- Using - or * instead of + for strings
- Missing semicolon at end
- Trying to divide or multiply strings
string a = "Hello";
string b = "World";
string c = a + ", " + b + "!";
Console.WriteLine(c);
Solution
Step 1: Analyze string concatenation parts
The code joins "Hello", ", ", "World", and "!" in order, so the result is "Hello, World!".Step 2: Confirm output of Console.WriteLine
Console.WriteLine prints the combined string exactly as concatenated.Final Answer:
Hello, World! -> Option AQuick Check:
Strings + punctuation join as typed [OK]
- Missing spaces or commas in output
- Ignoring punctuation strings
- Confusing concatenation order
string first = "Good";
string second = "Morning";
string message = first + second;
message += 5;
Console.WriteLine(message);
Solution
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".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.Final Answer:
Missing space between first and second strings. -> Option AQuick Check:
Check spaces when joining strings [OK]
- Assuming += 5 causes error
- Ignoring missing spaces in output
- Thinking Console.WriteLine is wrong
string[] words = {"apple", "banana", "cherry"};, which code correctly concatenates them into a single comma-separated string using string.Concat?Solution
Step 1: Understand string.Concat usage
string.Concatcan join multiple strings passed as arguments. To add commas, they must be separate arguments.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.Final Answer:
string result = string.Concat(words[0], ",", words[1], ",", words[2]); -> Option DQuick Check:
Pass each string and separator separately to Concat [OK]
- Trying to add commas inside array
- Passing array directly to Concat without separators
- Using + inside Concat argument incorrectly
