Skip to content

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.

Strings represent text; booleans represent decisions. Text is indexed from zero, and a C# string is immutable: operations return new strings.

Use + to join strings or $"...{expression}..." to embed values. Interpolation tends to remain easier to read when formatting several values.

Example.cs
string player = "Alex";
int score = 25;
string oldWay = player + ": " + score;
string message = $"{player}: {score} points";
Console.WriteLine(message);

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.

Example.cs
string label = "Level";
char first = label[0];
string multiline = "Line one\nLine two";
string path = @"C:\Games\Save.txt";
Console.WriteLine($"{first}, {label.Length}");

A bool holds either true or false. C# does not implicitly treat integers as booleans: if (1) is invalid. Comparison expressions produce boolean results.

Example.cs
bool isReady = true;
int lives = 0;
bool canPlay = isReady && lives > 0;
Console.WriteLine(canPlay); // False

Deep dive: characters, text, and decisions

Section titled “Deep dive: characters, text, and decisions”

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.

Program.cs
string original = " Alex ";
string cleaned = original.Trim().ToUpperInvariant();
Console.WriteLine($"[{original}]"); // [ Alex ]
Console.WriteLine(cleaned); // ALEX

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.

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.

Prefer interpolation for output, and explicit booleans for flags with clear names such as isActive or hasPermission.

Indexing past the end throws an exception; remember that Unicode emoji may occupy more than one UTF-16 code unit.

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);.