Practice mode C#

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.