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 enablestring? maybeNull = null; // allowedstring notNull = "text";notNull = maybeNull; // warningMethod parameters and return types
Section titled “Method parameters and return types”public string? FindName(int id) => id == 0 ? null : "John";public void Process(string name) { }// Calling Process(FindName(0)) -> warning: possible null argumentReal-world usage example
Section titled “Real-world usage example”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.