0
0
C Sharp (C#)programming~5 mins

String concatenation behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

String concatenation lets you join two or more pieces of text into one. It helps build messages or combine words easily.

When you want to create a full sentence from separate words or phrases.
When you need to add a user's name into a greeting message.
When combining file paths or URLs from parts.
When building output that shows multiple values together.
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
Joins two string literals into one.
C Sharp (C#)
string hello = "Hello, " + "world!";
Joins a variable with strings to make a greeting.
C Sharp (C#)
string name = "Alice";
string greeting = "Hi " + name + "!";
Uses 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);
    }
}
OutputSuccess
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.