What do ??, ??= and ?. do in C#?
Quick Answer
a ?? b — return a unless it's null, then b. a ??= b — assign b to a only if a is currently null. a?.b — access b only if a isn't null, otherwise the whole expression is null.
?? is a null fallback — x ?? y gives me x unless it's null, then y. ??= only assigns if the left side is currently null, which is handy for lazy initialisation. And ?. is null-conditional access — obj?.Property gives null instead of throwing if obj is null, and it chains, so a?.b?.c stops at the first null. A nice combination is value ?? throw new ArgumentNullException(...) for a one-line guard clause.
name = input ?? throw new ArgumentNullException(nameof(input));— the throw-expression idiom for guard clauses.list?.Count ?? 0— safe navigation plus default._cache ??= BuildCache();— lazy one-time init.a?.b?.c()short-circuits at the first null;obj?.Method()won't call the method ifobjis null.