/errors/csharp/cs8618-non-nullable-property-must-contain-non-null-value
C# warning CS8618: Non-nullable property must contain a non-null value when exiting constructor
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
- tool
- C#
- exit code
- 0
- versions
- C# 8+ with nullable reference types, .NET 6 – 10 (enabled by default in new projects)
- os
- any
warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor comes from C# nullable reference types. The property is declared as string (meaning "never null"), but after the constructor finishes nothing has assigned it, so it is still null. Mark it required, give it an initial value, set it in the constructor, or declare it string? if null is allowed.
Reproduce it
dotnet new console -o app >/dev/null 2>&1
cd app
cat > Program.cs <<'EOF'
var u = new User { Name = "Ada" };
Console.WriteLine(u.Name);
class User
{
public string Name { get; set; }
}
EOF
dotnet build -nologo 2>&1 | grep -o 'warning CS8618: [^[]*' | sort -uOutput
warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
The build succeeds: CS8618 is a warning. Projects with <TreatWarningsAsErrors>true</TreatWarningsAsErrors> (or <WarningsAsErrors>nullable</WarningsAsErrors>) turn it into error CS8618 and the build fails.
Fix: required, initializer, constructor or nullable
dotnet new console -o app >/dev/null 2>&1
cd app
cat > Program.cs <<'EOF'
var u = new User { Name = "Ada" };
var o = new Order("A-1");
Console.WriteLine($"{u.Name} {u.Email.Length} {o.Id} {u.Nickname ?? "(none)"}");
class User
{
public required string Name { get; set; } // callers must set it
public string Email { get; set; } = string.Empty; // default value
public string? Nickname { get; set; } // null is allowed
}
class Order(string id) // primary constructor (C# 12)
{
public string Id { get; } = id;
}
EOF
dotnet build -nologo 2>&1 | grep -c 'CS8618'
dotnet run --no-buildOutput
0 Ada 0 A-1 (none)
| Fix | Use when |
|---|---|
| required string Name | The caller must always provide the value (C# 11+) |
| string Name = string.Empty | An empty value is a sensible default |
| Assign in the constructor | The value is a constructor argument |
| string? Name | The value is genuinely optional |
| string Name = null! | A framework sets it later; silences the warning without any check |
Sources
Related pages
errors · csharpC# error CS0246: The type or namespace name could not be foundCS0246 means the C# compiler cannot resolve a type or namespace: a missing using directive, NuGet package or project reference. Real dotnet build output.commands · dotnet-clidotnet new console: create and run a C# console appdotnet new console -o MyApp creates a C# console project with top-level statements, and dotnet run builds and starts it. Options and other common templates.errors · dotnet-clierror NETSDK1045: The current .NET SDK does not support targeting .NET XNETSDK1045 means the project's TargetFramework is newer than the installed .NET SDK supports. Install a newer SDK, fix global.json, or lower TargetFramework.flags · csharpC# Nullable=enable (dotnet build -p:Nullable): nullable reference type warningsC# Nullable=enable turns on nullable reference type warnings like CS8602 and CS8618. The four values enable, warnings, annotations, disable, and how to migrate.