Read the syntax
A string is immutable text: operations such as Trim, Replace and ToUpper return another string. Concatenation uses +, while $"...{expression}..." evaluates expressions inside braces.
The @ prefix makes a verbatim string in which backslashes are literal; triple quotes form a raw string literal in C# 11 or later. Format specifiers after a colon, such as F2, affect how an interpolated value is displayed.
Syntax
string variableName = $"text {expression}";Examples
Creating and Combining Strings
Basic string creation and concatenation.
string firstName = "John";
string lastName = "A";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName); // John A
string greeting = string.Format("Hello, {0}!", firstName);
Console.WriteLine(greeting);- Basic string creation and concatenation.
- A string is immutable text: operations such as Trim, Replace and ToUpper return another string. Concatenation uses +, while $"...{expression}..." evaluates expressions inside braces.
- The + expressions join firstName, a space and lastName to print John A. string.Format substitutes the argument for {0}, producing Hello, John!.
String Interpolation
The idiomatic way to embed expressions and format values inside a string.
string name = "John";
int age = 21;
decimal price = 19.5m;
Console.WriteLine($"{name} is {age} years old");
Console.WriteLine($"Price: ${price:F2}"); // Price: $19.50 in an en-US culture
Console.WriteLine($"{name.ToUpper()} says hi"); // expressions work too- The idiomatic way to embed expressions and format values inside a string.
- A string is immutable text: operations such as Trim, Replace and ToUpper return another string. Concatenation uses +, while $"...{expression}..." evaluates expressions inside braces.
- The first line prints John is 21 years old. F2 displays price with two decimal places, and ToUpper produces JOHN; numeric punctuation can vary with the current culture.
Multi-line and Verbatim Strings
The @ prefix creates a verbatim string where escape sequences aren't processed, ideal for file paths and multi-line text.
string path = @"C:\Users\John\Documents"; // no need to double-escape backslashes
Console.WriteLine(path);
string multiLine = @"Line one
Line two
Line three";
Console.WriteLine(multiLine);
// Raw string literals (C# 11+) go even further
string json = """{"name": "John"}""";
Console.WriteLine(json);- The @ prefix creates a verbatim string where escape sequences aren't processed, ideal for file paths and multi-line text.
- A string is immutable text: operations such as Trim, Replace and ToUpper return another string. Concatenation uses +, while $"...{expression}..." evaluates expressions inside braces.
- The verbatim path preserves backslashes, and the multiline literal preserves its line breaks. The raw string stores the JSON punctuation without escaping its quotation marks.
Common String Methods
Length, case conversion, trimming, and substrings.
string text = " Hello World ";
Console.WriteLine(text.Length); // 15 (includes whitespace)
Console.WriteLine(text.Trim()); // "Hello World"
Console.WriteLine(text.ToUpper()); // " HELLO WORLD "
Console.WriteLine(text.Trim().Substring(0, 5)); // "Hello"
Console.WriteLine(text.Contains("World")); // true- Length, case conversion, trimming, and substrings.
- A string is immutable text: operations such as Trim, Replace and ToUpper return another string. Concatenation uses +, while $"...{expression}..." evaluates expressions inside braces.
- Length counts all characters, including four surrounding spaces; the sample length is 15. Trim removes those spaces, ToUpper returns uppercase text, and Substring selects Hello from the trimmed copy.
Best practices
- Use string interpolation ($"...") instead of string.Format() or + concatenation - it is more readable and just as efficient
- Use a StringBuilder instead of repeated + concatenation when building a large string inside a loop, for much better performance
- Use verbatim strings (@"...") for file paths and regular expressions to avoid a wall of doubled backslashes
- Remember string is immutable - every "modifying" method like .Trim() or .ToUpper() returns a new string rather than changing the original
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference