Learning outcome
By the end of this lesson, you should be able to choose between bool, char, and string with confidence, use them correctly in conditions and text processing, and avoid the mistakes that cause brittle validation and subtle bugs.
Intuition
These three types look simple, but they solve different problems:
boolrepresents a yes/no state.charrepresents one UTF-16 code unit, often a single displayed character for basic text.stringrepresents a sequence of characters, which is how you store names, messages, identifiers, and user input.
In real applications, you constantly combine them. For example, a login form may store a bool for whether the password is valid, inspect char values when validating input, and use string for the full username or email address.
A practical rule is: use the smallest type that correctly models the data. If the value can only be true or false, use bool. If you need one character, use char. If you need text, use string.
Deep dive
bool
bool has only two values: true and false. It is the natural result of comparisons and logical checks.
Use booleans for state and decisions, not for encoding magic numbers or strings like "yes" and "no". That keeps code readable and safer.
char
char stores one UTF-16 code unit and is written in single quotes.
A common beginner mistake is treating char as if it always means a full user-perceived character. Most everyday ASCII letters fit well, but some Unicode characters may require more than one code unit. For professional code, remember that char is a building block for text, not always the full story of what a human sees.
string
string stores text and is written in double quotes.
Strings are immutable, meaning each apparent change creates a new string. That matters when building text repeatedly. For small amounts of text, simple concatenation is fine. For larger or repeated composition, prefer helpers like interpolation or StringBuilder when appropriate.
Comparing text and characters
- Use
==for value comparison withstringandcharin everyday code. - Avoid reference-equality assumptions for strings.
- Use culture-aware or ordinal comparisons intentionally when correctness depends on user language or protocol rules.
Reading user input safely
Console input often arrives as a string, even when you need a bool-like decision.
Failure modes
- Using
stringwhereboolis appropriate: this makes validation harder and encourages invalid states. - Confusing
charandstring:'A'and"A"are different types and are not interchangeable. - Assuming every visible character is one
char: Unicode text can be more complex than a single code unit. - Comparing strings without thinking about comparison rules: protocol text, keys, and identifiers often need ordinal comparison; user-facing text may need culture-aware handling.
- Ignoring immutability: repeated string concatenation in loops can hurt performance.
Interview drill
Explain the difference between char and string. When would you choose bool over string for a field? What does string immutability mean, and why does it matter? Why can Unicode make character handling trickier than it first appears?
Revision checklist
- I can declare and initialize
bool,char, andstringvalues. - I know that
true/falsearebool, single quotes are forchar, and double quotes are forstring. - I can use boolean expressions in
ifstatements. - I understand that strings are immutable.
- I can explain why Unicode can make character processing more advanced than basic ASCII.
- I compare text intentionally instead of by accident.
Production code
A realistic console app often validates text input, uses booleans for decisions, and inspects characters before accepting data.
Code walkthrough
userNameis astringbecause a name is text, not a single character.userName[0]reads the firstcharin the string.char.IsUpper(firstLetter)returns aboolthat feeds into validation.string.IsNullOrWhiteSpace(userName)protects against empty or blank input.- The final
boolcombines both checks, which is a common pattern in production code.
For the broader curriculum, this lesson sits in the types and expressions module and supports later topics like control flow, validation, and input handling. The same ideas will reappear in collection keys, parsing, testing, and API design.
Fact-check notes
bool,char, andstringare core C# types and remain valid in .NET 10 / C# 14.stringis immutable in .NET.charrepresents a UTF-16 code unit, not always a full Unicode scalar or user-perceived grapheme.- String comparison behavior depends on the comparison method used; choice of ordinal vs culture-aware comparison is version-stable but important to apply intentionally.
<!-- tin:verified-code:start -->
Executable code examples
Validating text with bool, char, and string
Program.cs
using System;
string userName = "Mina";
char firstLetter = userName[0];
bool startsWithUppercase = char.IsUpper(firstLetter);
bool isValidName = !string.IsNullOrWhiteSpace(userName) && startsWithUppercase;
Console.WriteLine($"Name: {userName}");
Console.WriteLine($"First letter: {firstLetter}");
Console.WriteLine($"Valid: {isValidName}");
<!-- tin:verified-code:end -->