CodeBlanks Logo Icon CodeBlanks

String concatenation

C# course · Beginner · Lesson 32/116

Learn to join text with the + operator and string.Concat.

String concatenation

To put two strings end to end you use the + operator: "Alex" + " " + "Smith" yields a single string. This is called concatenation.

During concatenation you must add separators like spaces yourself; C# does not insert them automatically. The string.Concat(a, b) method gives the same result and works for joining most strings side by side.

Joining a number to text

If you concatenate a string and a number with +, C# converts the number to text automatically: "Age: " + 25 becomes "Age: 25". This convenience helps when designing output.

Still, to avoid confusion, modern C# code prefers interpolation: $"Age: {age}" reads more clearly. Concatenation stays practical for short, two-piece cases.

Exercise Concatenate first and last name

Declare first and last variables. Build full by joining the first name, a space and the last name with the + operator. Then print full with Console.WriteLine.

Show example solution
Assign first + " " + last to the full variable and print it.
Example solution:
using System;

class Program
{
    static void Main()
    {
        string first = "Alex";
        string last = "Smith";
        string full = first + " " + last;
        Console.WriteLine(full);
    }
}

Test yourself

  1. Match each concatenation to its output

    "sa" + "m"
    sam
    "Sa" + "m"
    Sam
    "a" + "b" + "c"
    abc
    "x" + " " + "y"
    x y
  2. What is the result of "5" + "3"?

    string result = "5" + "3";
    • 8
    • 53
    • "8"
    • 53.0
    Show answer

    "8" — Correct. The quoted 5 and 3 are strings, so they are joined: "53".

  3. Add the space with concatenation

    string first = "Alex";
    string last = "Smith";
    string full = first ____ " " + last;
    Show answer
    string first = "Alex";
    string last = "Smith";
    string full = first + " " + last;

All lessons in Beginner