Object-Oriented C# · Lesson 7

Nullable references

A string? may be null. ?.Length does not call Length on null.

How it works

name starts as null. name?.Length is null, and ?? 0 turns that into 0, so the first line is 0. After name is Ada, name.Length is 3. The ? is what skips the member while the value is missing.

A working example

string? name = null;
Console.WriteLine(name?.Length ?? 0);
name = "Ada";
Console.WriteLine(name.Length);

Try this
Use ?. when the string may be null.