What are nullable reference types (NRTs)?
string name = "x"; // must not be null
string? middle = null; // may be null6 Nullable Reference Types interview questions for C# roles, each with a model answer, examples and likely follow-ups.
6 questionsWhat are nullable reference types (NRTs)?
string name = "x"; // must not be null
string? middle = null; // may be nullWhat is the null-forgiving operator (!) and when is it appropriate?
var name = dict[key]!; // you guarantee the key existsWhat is the difference between ?., ?? and ??=?
int len = name?.Length ?? 0;
cache ??= new();How do nullable annotations affect API design?
public User? FindById(int id); // may return null
public string GetName(User u); // never nullWhat are nullable attributes like NotNullWhen and MaybeNull for?
bool TryGet(string key, [NotNullWhen(true)] out User? user);How do you adopt NRTs safely in a large existing codebase?
#nullable enable // start file-by-file