Strings, characters and booleans
↑Examples are independent C# snippets for modern .NET console projects unless noted otherwise. Class declarations and top-level code can live in Program.cs when top-level statements come first.
At a glance
Section titled “At a glance”Strings represent text; booleans represent decisions. Text is indexed from zero, and a C# string is immutable: operations return new strings.
How it works
Section titled “How it works”Concatenation and interpolation
Section titled “Concatenation and interpolation”Use + to join strings or $"...{expression}..." to embed values. Interpolation tends to remain easier to read when formatting several values.
string player = "Alex";int score = 25;string oldWay = player + ": " + score;string message = $"{player}: {score} points";Console.WriteLine(message);Indexes, characters and escaping
Section titled “Indexes, characters and escaping”text[0] accesses the first UTF-16 code unit. Length counts UTF-16 code units, not necessarily user-perceived characters. Use \n for a newline, \" for a quote, or verbatim @ strings for paths.
string label = "Level";char first = label[0];string multiline = "Line one\nLine two";string path = @"C:\Games\Save.txt";Console.WriteLine($"{first}, {label.Length}");Boolean values
Section titled “Boolean values”A bool holds either true or false. C# does not implicitly treat integers as booleans: if (1) is invalid. Comparison expressions produce boolean results.
bool isReady = true;int lives = 0;bool canPlay = isReady && lives > 0;Console.WriteLine(canPlay); // FalseDeep dive: characters, text, and decisions
Section titled “Deep dive: characters, text, and decisions”Strings are immutable
Section titled “Strings are immutable”A string represents a sequence of UTF-16 code units. Operations such as Replace, Trim, ToUpper and Substring return a new string (or an existing equivalent); they do not modify the original variable’s value unless you assign the result. This makes strings convenient to share, but repeated concatenation in a tight loop can produce many temporary allocations. Use StringBuilder for large repeated edits.
string original = " Alex ";string cleaned = original.Trim().ToUpperInvariant();Console.WriteLine($"[{original}]"); // [ Alex ]Console.WriteLine(cleaned); // ALEXchar is not always one visible character
Section titled “char is not always one visible character”text[0] reads a char, which is a UTF-16 code unit. Some Unicode symbols such as many emoji require two code units, and some visible characters combine several code points. For simple ASCII-like identifiers this distinction rarely matters; for multilingual user input it does. Prefer Unicode-aware APIs when manipulating user-visible graphemes instead of treating each char as a complete character.
Interpolation keeps values and text separate
Section titled “Interpolation keeps values and text separate”$"Score: {score}" embeds an expression and formats its result. Use interpolation for readable messages, and format numeric/time values intentionally when the output is user-facing. Escape a literal brace with {{ or }} inside an interpolated string. When building SQL queries or shell commands, do not simply interpolate untrusted input; use the target system’s parameterized APIs.
Booleans describe conditions
Section titled “Booleans describe conditions”A bool is either true or false. Use names like isAlive, hasKey or canJump to describe a condition, and combine conditions with &&, || and !. Avoid unnecessary comparisons such as if (isAlive == true); if (isAlive) communicates the same test more clearly. Distinguish a stored flag from a condition that can be derived from current state.
When and why
Section titled “When and why”Prefer interpolation for output, and explicit booleans for flags with clear names such as isActive or hasPermission.
Common mistake
Section titled “Common mistake”Indexing past the end throws an exception; remember that Unicode emoji may occupy more than one UTF-16 code unit.
Try it yourself
Section titled “Try it yourself”Print a player name and number of lives using interpolation, then print whether lives are above zero.
Reveal a solution
Console.WriteLine($"{name}: {lives}"); Console.WriteLine(lives > 0);.
