C# "CS8618: non-nullable field/property must contain a non-null value" in CI
CS8618 is a nullable-reference-types diagnostic: a non-nullable member is not guaranteed to be assigned by the time the constructor finishes. With nullable enabled and warnings treated as errors, it fails the build even though it would only warn locally.
What this error means
The build fails with CS8618 naming the uninitialized member. It is deterministic and often appears only in CI because CI treats warnings as errors.
dotnet
Models/User.cs(8,19): error CS8618: Non-nullable property 'Email' must contain a non-null
value when exiting constructor. Consider declaring it as nullable.Common causes
A non-nullable member is never assigned
A reference-type property or field has no initializer and is not set in every constructor path, so the compiler cannot prove it is non-null.
CI treats nullable warnings as errors
TreatWarningsAsErrors (or WarningsAsErrors including nullable) turns the local warning into a hard CI failure.
How to fix it
Guarantee the member is initialized
- Assign the member in the constructor, give it a default, or use the
requiredmodifier. - For DTOs deserialized later, initialize with
= null!or= default!only if the contract truly guarantees population. - Rebuild.
C#
public required string Email { get; init; }How to prevent it
- Enable nullable reference types early so these are caught during development, not only in CI.
- Use
requiredmembers for values that must be supplied by callers.
Related guides
C# "CS8600: converting null literal to non-nullable type" in CIFix C# "CS8600: Converting null literal or possible null value to non-nullable type" in CI - a nullable-refer…
C# "CS8602: Dereference of a possibly null reference" in CIFix C# "CS8602: Dereference of a possibly null reference" in CI - a nullable-reference warning promoted to an…
dotnet Nullable Warnings as Errors (CS8600, CS8602) in CIFix dotnet builds failing on nullable reference warnings (CS8600, CS8602, CS8618) promoted to errors in CI -…