Codectionary / Developer documentation / C#

Strings & String Interpolation

string represents an immutable sequence of characters in C# - once created, its content never changes, and every 'modifying' operation returns a new string. String interpolation, using the $ prefix, is the modern, preferred way to embed expressions directly inside a string literal, replacing older approaches like string.Format() or manual concatenation with +.

Syntax

string variableName = $"text {expression}";

Examples

Creating and Combining Strings

Basic string creation and concatenation.

string firstName = "Fola";
string lastName = "A";

string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);  // Fola A

string greeting = string.Format("Hello, {0}!", firstName);
Console.WriteLine(greeting);

String Interpolation

The idiomatic way to embed expressions and format values inside a string.

string name = "Fola";
int age = 21;
double price = 19.5;

Console.WriteLine($"{name} is {age} years old");
Console.WriteLine($"Price: ${price:F2}");        // Price: $19.50
Console.WriteLine($"{name.ToUpper()} says hi");   // expressions work too

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\Fola\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": "Fola"}""";
Console.WriteLine(json);

Common String Methods

Length, case conversion, trimming, and substrings.

string text = "  Hello World  ";

Console.WriteLine(text.Length);          // 17 (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

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.