Skip to content

Nullable warnings

Context: When nullable annotations are enabled, the compiler produces warnings for potentially unsafe null operations.

  • CS8618: Non‑nullable property uninitialized
  • CS8600: Converting null literal to non‑nullable type
  • CS8602: Dereference of a possibly null reference
  • CS8625: Cannot convert null literal to non‑nullable type
public class Person
{
public string Name { get; set; } // CS8618
}
// Fix: initialize
public class Person
{
public string Name { get; set; } = "";
}
string? name = GetName();
if (name != null)
{
int length = name.Length; // no warning
}
// Or using null‑conditional operator
int? length = name?.Length;

Team coding standards: Enforce nullable warnings as errors in CI/CD to prevent null reference exceptions in production.

Example: In a GitHub Actions workflow, add -p:WarningsAsErrors=nullable to dotnet build.