Skip to content

Non-nullable property warning (CS8618): net10.0, MSTest 4, MemberNotNull and field keyword samples - #2293

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/79953-csharp-resolve-non-nullable-property-must-contain-a-non-null-value-warning
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/79953-csharp-resolve-non-nullable-property-must-contain-a-non-null-value-warning

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "Non-nullable Property Must Contain a Non-null Value When Exiting Constructor (CS8618)" (https://code-maze.com/csharp-resolve-non-nullable-property-must-contain-a-non-null-value-warning/).

Folder: csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning (only this folder changes).

Changes:

  • Both projects retargeted from net7.0 to net10.0. Nullable stays enabled.
  • Packages, re-queried on NuGet 2026-10-11 (latest listed stable): Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter 4.5.1, MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0.
  • MSTest 4 removed [ExpectedException], so the dereference test now calls Assert.ThrowsExactly and dereferences _address! (the old _address? would have let the test pass vacuously on a null field).
  • Both #pragma comments in Address.cs now carry the current CS8618 diagnostic title from the .NET 10 compiler: "Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable."
  • Address gains Region, a field-keyword property inside the CS8618 pragma block, which still reports CS9264 (the pragma does not cover it), and Town, the field-keyword property resolved with get => field ?? string.Empty.
  • New HelperMethod/Address.cs shows [MemberNotNull(nameof(City))] on a helper method shared by two constructors.
  • Four new tests: Region is null, Town is empty, and the helper class sets City from both constructors.

Build (SDK 10.0.302, Release): 0 errors, 2 warnings, both deliberate: CS8618 on Address.FirstName and CS9264 on Address.Region. These are the article's demonstrations.
Tests: 11 passed, 0 failed.
dotnet list package --vulnerable --include-transitive: no vulnerable packages in either project.

…ssage text, MemberNotNull and field keyword samples

- Retarget both projects from net7.0 to net10.0.
- Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0.
- MSTest 4 removed [ExpectedException]: the dereference test now uses Assert.ThrowsExactly<NullReferenceException> with _address! instead of _address?.
- Both #pragma comments now carry the current CS8618 title ("Consider adding the 'required' modifier or declaring as nullable.").
- Address gains Region, a field-keyword property inside the CS8618 pragma that still reports CS9264, and Town, the field-keyword property resolved in its get accessor.
- New HelperMethod.Address shows [MemberNotNull] on a helper shared by two constructors.
- Four new tests, one per new member and constructor.
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.

1 participant