Skip to content
Latchkey

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

  1. Assign the member in the constructor, give it a default, or use the required modifier.
  2. For DTOs deserialized later, initialize with = null! or = default! only if the contract truly guarantees population.
  3. 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 required members for values that must be supplied by callers.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →