warning CS8618: Non-nullable property 'X' must contain a non-null value when exiting constructor. Consider declaring the property as nullable

With nullable reference types enabled, a non-nullable reference property isn’t initialised by the constructor.

Exact message you may see Non-nullable property must contain a non-null value when exiting constructorConsider adding the 'required' modifier or declaring the property as nullable
Seen on: .NET

Meaning

It’s a warning (often treated as error). Choose: initialise it, make it nullable (string?), use required (C# 11), or = null! when another mechanism (EF, DI) sets it.

Common causes

  • Property not assigned in constructor
  • DTO/entity populated by deserializer/EF
  • TreatWarningsAsErrors turning it into a build failure

⚡ Quick fix

  1. Use the required modifier
  2. Initialise with a default (= string.Empty)
  3. Make it nullable if null is valid

Detailed fix by platform

C#

  1. csharp
    public class UserDto
    {
        public required string Email { get; init; }
        public string DisplayName { get; set; } = string.Empty;
        public string? Phone { get; set; }
    }

How to diagnose

  1. Semantics — Can it be null?
  2. Population — Who sets it?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

CS0103 — error CS0103: The name 'x' does not exist in the current context (.NET)CS0103CS0246 — error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?) (.NET)CS0246CS1061 — error CS1061: 'X' does not contain a definition for 'Y' and no accessible extension method 'Y' accepting a first argument of type 'X' could be found (.NET)CS1061declared and not used — Go: declared and not used: x / "fmt" imported and not used (Go)declared and not usedE0308 — Rust: error[E0308]: mismatched types — expected `X`, found `Y` (Rust)E0308NullReferenceException — System.NullReferenceException: Object reference not set to an instance of an object (.NET)NullReferenceExceptionThe request matched multiple endpoints — AmbiguousMatchException: The request matched multiple endpoints. Matches: X, Y (.NET)The request matched m…Type of expression is ambiguous — Swift: Type of expression is ambiguous without a type annotation (more context) (iOS)Type of expression is…CS8618

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.