Skip to content

fix: resolve CS8607 for ValueObject with nullable value type underlying#931

Merged
SteveDunn merged 2 commits into
mainfrom
bugfix/914-nullable-value-type-gethashcode-cs8607
May 6, 2026
Merged

fix: resolve CS8607 for ValueObject with nullable value type underlying#931
SteveDunn merged 2 commits into
mainfrom
bugfix/914-nullable-value-type-gethashcode-cs8607

Conversation

@SteveDunn

Copy link
Copy Markdown
Owner

When the underlying type is a nullable value type (e.g. ushort?), EqualityComparer.Default.GetHashCode gained a [DisallowNull] annotation on its parameter in .NET 10+. Passing a Nullable value to it triggers compiler warning CS8607.

Fix: detect when the underlying type IsNullableValueType() and emit
Value is null ? 0 : Value.GetHashCode()
instead of routing through EqualityComparer. This is null-safe and avoids the DisallowNull constraint entirely.

Applied to both GenerateForAStruct and GenerateGetHashCodeForAClass. The class variant wraps in parentheses to ensure correct ^ precedence.

Fixes #914

SteveDunn and others added 2 commits May 5, 2026 22:07
When the underlying type is a nullable value type (e.g. ushort?),
EqualityComparer<T>.Default.GetHashCode gained a [DisallowNull] annotation
on its parameter in .NET 10+. Passing a Nullable<T> value to it triggers
compiler warning CS8607.

Fix: detect when the underlying type IsNullableValueType() and emit
  Value is null ? 0 : Value.GetHashCode()
instead of routing through EqualityComparer. This is null-safe and
avoids the DisallowNull constraint entirely.

Applied to both GenerateForAStruct and GenerateGetHashCodeForAClass.
The class variant wraps in parentheses to ensure correct ^ precedence.

Fixes #914

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…hCode

Includes generated snapshots for handling nullable value types in struct and class variants, utilizing null-safe `GetHashCode` logic to avoid `DisallowNull` warnings.
@SteveDunn
SteveDunn merged commit a4c83cf into main May 6, 2026
1 of 5 checks passed
@SteveDunn
SteveDunn deleted the bugfix/914-nullable-value-type-gethashcode-cs8607 branch May 6, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueObject<short?> generates code with CS8607 warning - possible NullReferenceException?

1 participant