Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
string? name = null;
Console.WriteLine(name?.Length ?? -1);
name ??= "Anna";
name ??= "Bob";
Console.WriteLine(name);-1 Anna
- Expecting
-1 Bob:??=assigns only when the variable holdsnull, so the second line with??=leaves"Anna"in place. - Expecting
0or an exception: withnullinname,name?.Lengthgivesnullwithout touchingLength, and??turns it into-1.
- What would the program print if the first line were
string? name = "Bob";? - What is the type of the expression
name?.Length, and why is it notint?