Null-aware and conditional operators
Null-aware operators help handle absent values without confusing absence with an empty string or zero. The conditional operator selects a value based on a Boolean condition. These tools keep simple fallback logic concise while preserving meaningful data.
int? recorded = 0;int? missing = null;System.Console.WriteLine(recorded ?? 10);System.Console.WriteLine(missing ?? 10);The fallback is used only for the missing value, not for recorded zero.
Expected output (reasoned, not executed):
010Fallback only for null
A nullable value type such as int? can hold an int or null. A nullable reference annotation such as string? permits a possibly missing reference under nullable analysis. value ?? fallback evaluates the fallback only when value is null. An empty string and the integer zero are not null. value ??= fallback updates a variable only if it is null.
Access a member when present
label?.Length returns the string length when label exists; otherwise it returns null (an int? here). A property is a named value exposed by a type; properties receive fuller treatment later. Combine this with ?? to obtain an ordinary int. The operator does not suppress errors from operations on a non-null receiver.
Select a value using a condition
condition ? whenTrue : whenFalse evaluates exactly one result expression, based on the bool condition. Use compatible branch types, and keep both alternatives readable. This is an expression that yields a value, unlike the if statement in the next chapter. Avoid nested conditional expressions for multi-step decisions.
Examples and walkthroughs
string? label = null;System.Console.WriteLine(label?.Length ?? 0);label ??= "Shelf A";System.Console.WriteLine(label);System.Console.WriteLine(label?.Length ?? 0);The first access produces null and falls back to zero. Assignment supplies a label, whose length is seven.
Expected output (reasoned, not executed):
0Shelf A7int stock = 0;string message = stock > 0 ? "Available" : "Sold out";System.Console.WriteLine(message);string? label = "";System.Console.WriteLine(label ?? "Missing");System.Console.WriteLine("End");The conditional chooses Sold out. The second line is blank because empty text is present, not null.
Expected output (reasoned, not executed):
Sold out
EndCommon mistake and correction
Do not use ?? to replace whitespace or zero unless null is actually the missing-value marker. A chain such as label?.Length ?? 0 protects the access against a null label, but cannot distinguish missing from empty after the fallback.
Try it yourself
Given int? quantity, display 5 only when it is missing; otherwise display the recorded value. Predict results for null, zero and 8. Then choose "Restock" when the effective quantity is zero and "Ready" otherwise.
Next: Bitwise operators and shifts. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Null-aware and conditional operators — knowledge check
Apply the lesson concepts to the following scenarios.
What is (int?)0 ?? 5?
What does null string label?.Length produce?
What does label ??= "A" do when label is "B"?
Which expression selects the text "yes" when ready is true?
How many result branches of a conditional expression execute?