String concatenation lets you join two or more pieces of text into one. It helps build messages or combine words easily.
String concatenation behavior in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
string result = string1 + string2;
// or
string result = string.Concat(string1, string2);The + operator joins strings simply and clearly.
string.Concat is a method that also joins strings and can take many parts.
Examples
C Sharp (C#)
string hello = "Hello, " + "world!";
C Sharp (C#)
string name = "Alice"; string greeting = "Hi " + name + "!";
string.Concat to join multiple strings.C Sharp (C#)
string combined = string.Concat("C#", " ", "rocks!");
Sample Program
This program joins first and last names with a space using +. Then it creates a welcome message using string.Concat. It prints both results.
C Sharp (C#)
using System; class Program { static void Main() { string firstName = "John"; string lastName = "Doe"; string fullName = firstName + " " + lastName; Console.WriteLine("Full name: " + fullName); string message = string.Concat("Welcome, ", fullName, "!"); Console.WriteLine(message); } }
Important Notes
Using + for many strings repeatedly can be less efficient than StringBuilder for big tasks.
Strings in C# are immutable, so concatenation creates new strings each time.
Summary
String concatenation joins text pieces into one string.
You can use + operator or string.Concat method.
It is useful for building messages or combining data for display.
Practice
1. What does the
+ operator do when used between two strings in C#?easy
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]
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
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]
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
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]
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
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]
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
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]
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
