Boolean, char, string and null
Not every value is a number. C# uses bool for true or false, char for one UTF-16 code unit, and string for a sequence of UTF-16 code units. A null reference represents the absence of an object, not an empty string. These distinctions help you model information such as a book title, shelf label and availability.
string title = "Field notes";char aisle = 'B';bool available = true;System.Console.WriteLine(title);System.Console.WriteLine(aisle);System.Console.WriteLine(available);The title is text; the aisle is a single code unit; the availability is a logical value. Console writes bool as True or False.
Expected output (reasoned, not executed):
Field notesBTrueBoolean and character literals
Write true or false without quotes for a bool. Use single quotes for a char such as 'A', and double quotes for a string such as "A". A char stores a 16-bit UTF-16 code unit, not necessarily an entire user-perceived character. For example, some emoji need two UTF-16 code units, and a displayed letter can combine multiple code points. Text handling is revisited in the globalization chapter.
Text and absence
A string is a reference type, and string objects are immutable: changing a variable to a different string does not modify the original string object. With nullable reference analysis enabled, string? note = null; communicates that a missing value is allowed. string note = ""; holds a real empty string. Null and empty are different states. Nullable reference annotations are compiler analysis; they do not create a new runtime string type or guarantee that null never appears.
Nullable value types
A non-nullable int or bool cannot be assigned null. Write int? reservedCopies = null; when an integer may be missing, rather than inventing a sentinel such as -1 that could be confused with real data. The HasValue property reports whether a nullable value type contains a value. A value of zero is present, and is different from null. Use null-aware operations safely; these are covered next.
Examples and walkthroughs
string? missing = null;string empty = "";System.Console.WriteLine(missing == null);System.Console.WriteLine(empty == null);The equality operator == asks whether the values match. The missing reference equals null, but the empty string is a non-null string object.
Expected output (reasoned, not executed):
TrueFalseint? unknown = null;int? recorded = 0;System.Console.WriteLine(unknown.HasValue);System.Console.WriteLine(recorded.HasValue);HasValue is false for the missing number, but true for the recorded zero. A property is a named value exposed by a type.
Expected output (reasoned, not executed):
FalseTrueCommon mistake and correction
Do not use bool available = "true"; or char aisle = "B";. Quotation marks change the literal type. Do not access members of a potentially null reference without checking it. The compiler can warn, but execution can still throw a NullReferenceException.
Try it yourself
Model a fictional checkout with a title "Atlas", aisle C, availability false, and an unknown reservation count. Print the title, aisle, availability and whether a reservation count is present. Then set the count to zero and print presence again.
Next: Type inference and default values. These examples are static study material; actual C# compilation and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Boolean, char, string and null — knowledge check
Apply the lesson concepts to the following scenarios.
Which declaration stores a Boolean value?
What does char represent?
Which state describes an existing string with no text?
With int? copies = 0;, what is copies.HasValue?
What does string? express with nullable analysis enabled?