0
0
C Sharp (C#)programming~10 mins

Null-conditional operator in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Null-conditional operator
Start
Evaluate object
Is object null?
YesReturn null
No
Access member or method
Return member value or method result
End
The null-conditional operator checks if an object is null before accessing its member or method, returning null if the object is null to avoid errors.
Execution Sample
C Sharp (C#)
string? name = null;
int? length = name?.Length;
Console.WriteLine(length);
This code safely tries to get the length of a string that might be null, using the null-conditional operator.
Execution Table
StepExpression EvaluatedIs Null?Action TakenResult
1nameYes (null)Skip accessing Length, return nulllength = null
2Console.WriteLine(length)N/APrints empty lineOutput: (empty line)
💡 name is null, so name?.Length returns null, preventing a NullReferenceException
Variable Tracker
VariableStartAfter Step 1After Step 2
namenullnullnull
lengthunassignednullnull
Key Moments - 2 Insights
Why does length become null instead of causing an error when name is null?
Because the null-conditional operator (?.) checks if name is null before accessing Length. If name is null, it returns null immediately, as shown in execution_table step 1.
What happens if we remove the ? and write name.Length when name is null?
It causes a NullReferenceException error because the program tries to access Length on a null object, unlike the safe check in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of length after step 1?
AThrows error
B0
Cnull
DEmpty string
💡 Hint
Check the 'Result' column in execution_table row 1 where length is assigned.
At which step does the program avoid a NullReferenceException?
ABoth steps
BStep 1
CStep 2
DNever avoids
💡 Hint
Look at the 'Action Taken' in execution_table step 1 where null check happens.
If name was "hello", what would length be after step 1?
A5
Bnull
CThrows error
D0
💡 Hint
Think about what Length returns for a non-null string, referencing variable_tracker for length.
Concept Snapshot
Null-conditional operator (?.) safely accesses members or methods.
If the object is null, it returns null instead of throwing an error.
Syntax example: obj?.Member or obj?.Method().
Useful to avoid NullReferenceException.
Returns nullable type if member is value type.
Can chain multiple null-conditional operators.
Full Transcript
The null-conditional operator in C# helps avoid errors when accessing members or methods on objects that might be null. It checks if the object is null first. If it is, it returns null instead of trying to access the member, which would cause an error. For example, if a string variable 'name' is null, then 'name?.Length' returns null safely. This prevents a NullReferenceException. The execution table shows that when 'name' is null, the program skips accessing Length and assigns null to 'length'. Then printing 'length' outputs an empty line because it is null. This operator is very useful to write safer code without many explicit null checks.