Skip to content

string question mark nullable reference

Context: Append ? to a reference type to indicate that it may be null. Without ?, the type is considered non‑nullable.

#nullable enable
string? maybeNull = null; // allowed
string notNull = "text";
notNull = maybeNull; // warning
public string? FindName(int id) => id == 0 ? null : "John";
public void Process(string name) { }
// Calling Process(FindName(0)) -> warning: possible null argument

Database query results: A method that finds a user by ID returns User? (nullable) because the user might not exist. This forces the caller to handle the null case.

Example: In Entity Framework Core, FirstOrDefault() returns T? (nullable) for reference types.