What is the output of: string s = null; Console.WriteLine(s?.Length ?? -1);?
Correct answer C. -1
Explanation
The null-conditional operator ?. checks s before accessing Length. Because s is null, s?.Length evaluates to null (its type is int?) instead of throwing NullReferenceException. The ?? operator then replaces that null with -1. Combining ?. and ?? is the idiomatic way to read a member safely and supply a fallback in one expression.
Answer checked How we write and check questions
Discussion