dotnet / dotnet/sdk

Default property value not applied when using default constructor.

Open
#53,318 2 comments 0 reactions 0 assignees View on GitHub
untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

Consider this code where IsEmpty is defaulted to true.
```
public readonly record struct DbName
{
public const int MaxLength = 150;

[JsonConstructor]
private DbName(string name)
{
Name = name.Trim();
IsEmpty = string.IsNullOrWhiteSpace(Name);
}

public string Name { get; } = "";
public bool IsEmpty { get; } = true;

public static DbName Create(string name) => new DbName(name);
}
```

If I publish this to NuGet, then create the object using new DbName(). The IsEmpty value is set to FALSE.

This is the decompiled code:
```
public readonly record struct DbName
{
public const int MaxLength = 150;

[JsonConstructor]
private DbName(string name)
{
this.Name = "";
this.IsEmpty = true;
this.Name = name.Trim();
this.IsEmpty = string.IsNullOrWhiteSpace(this.Name);
}

public string Name { get; }

public bool IsEmpty { get; }

public static DbName Create(string name) => new DbName(name);
}
```

If I decompile it BEFORE publishing to NuGet, the code is correct in that decompiling produces:
public bool IsEmpty { get; } = true;
and
this.IsEmpty = true; is not visible in the private constructor.

### To Reproduce
The class above is the only reequired code.
My unit test checks for IsValid == false if created using new DbName(). And it fails when run against the NuGet version.

The NuGet version fails whether it's built by a GitHub Action or if I use the Pack command locally. Both are broken.

It continues to fails under 10.0.103.

### Further technical details
details of dotnet --info


NET SDK:
Version: 10.0.103
Commit: c2435c3e0f
Workload version: 10.0.102
MSBuild version: 18.0.11+c2435c3e0

Runtime Environment:
OS Name: Mac OS X
OS Version: 26.3
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/10.0.103/

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.

Host:
Version: 10.0.3
Architecture: arm64
Commit: c2435c3e0f

.NET SDKs installed:
10.0.101 [/usr/local/share/dotnet/sdk]
10.0.103 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 10.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.24 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
x64 [/usr/local/share/dotnet/x64]
registered at [/etc/dotnet/install_location_x64]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

### Dev Env
JetBrains Rider 2025.3.3
Build #RD-253.31033.136, built on February 19, 2026

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.