From 8e01295010899311d8479303c6b8cb04093817fa Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 11 Oct 2026 12:52:56 +0200 Subject: [PATCH] Non-nullable property warning (CS8618): net10.0, MSTest 4, current message 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 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. --- .../NonNullablePropertyWarning/Address.cs | 13 +++++++++++-- .../HelperMethod/Address.cs | 16 ++++++++++++++++ .../NonNullablePropertyWarning.csproj | 2 +- .../NonNullablePropertyWarningUnitTest.cs | 19 +++++++++++++++++-- .../Tests/Tests.csproj | 10 +++++----- 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/HelperMethod/Address.cs diff --git a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs index 7962100296..aec77c0ecb 100644 --- a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs +++ b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs @@ -4,9 +4,12 @@ public class Address { public string FirstName { get; set; } -#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. public string MiddleName { get; set; } -#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + + // A field-keyword property reports CS9264, which this CS8618 pragma does not suppress + public string Region { get; set => field = value; } +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. #nullable disable public string LastName { get; set; } @@ -19,5 +22,11 @@ public class Address public string PostalCode { get; set; } = null!; public required string Area { get; set; } + + public string Town + { + get => field ?? string.Empty; + set => field = value; + } } } \ No newline at end of file diff --git a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/HelperMethod/Address.cs b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/HelperMethod/Address.cs new file mode 100644 index 0000000000..b6e8f31ced --- /dev/null +++ b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/HelperMethod/Address.cs @@ -0,0 +1,16 @@ +using System.Diagnostics.CodeAnalysis; + +namespace NonNullablePropertyWarning.HelperMethod +{ + public class Address + { + public string City { get; set; } + + public Address() => SetCity(); + + public Address(string city) => SetCity(city); + + [MemberNotNull(nameof(City))] + private void SetCity(string? city = default) => City = city ?? "Default City"; + } +} diff --git a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/NonNullablePropertyWarning.csproj b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/NonNullablePropertyWarning.csproj index cfadb03dd5..9ed914b5ba 100644 --- a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/NonNullablePropertyWarning.csproj +++ b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/NonNullablePropertyWarning.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable diff --git a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/NonNullablePropertyWarningUnitTest.cs b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/NonNullablePropertyWarningUnitTest.cs index 475eaafd9e..709844fafd 100644 --- a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/NonNullablePropertyWarningUnitTest.cs +++ b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/NonNullablePropertyWarningUnitTest.cs @@ -1,6 +1,7 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using NonNullablePropertyWarning; +using HelperAddress = NonNullablePropertyWarning.HelperMethod.Address; namespace Tests { @@ -16,8 +17,8 @@ public void Setup() } [TestMethod] - [ExpectedException(typeof(NullReferenceException))] - public void WhenDereferencingFirstName_ThenExceptionThrown() => _ = _address?.FirstName.Length; + public void WhenDereferencingFirstName_ThenExceptionThrown() => + Assert.ThrowsExactly(() => _ = _address!.FirstName.Length); [TestMethod] public void WhenInstantiatingAddress_ThenFirstNameNull() => Assert.IsNull(_address?.FirstName); @@ -36,5 +37,19 @@ public void Setup() [TestMethod] public void WhenInstantiatingAddress_ThenPostalCodeNull() => Assert.IsNull(_address?.PostalCode); + + [TestMethod] + public void WhenInstantiatingAddress_ThenRegionNull() => Assert.IsNull(_address?.Region); + + [TestMethod] + public void WhenInstantiatingAddress_ThenTownEmpty() => Assert.AreEqual(string.Empty, _address?.Town); + + [TestMethod] + public void WhenInstantiatingHelperAddressWithoutCity_ThenCityDefault() => + Assert.AreEqual("Default City", new HelperAddress().City); + + [TestMethod] + public void WhenInstantiatingHelperAddressWithCity_ThenCitySet() => + Assert.AreEqual("Belgrade", new HelperAddress("Belgrade").City); } } \ No newline at end of file diff --git a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/Tests.csproj b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/Tests.csproj index 0876be8a36..fc6c3cdc30 100644 --- a/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/Tests.csproj +++ b/csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/Tests/Tests.csproj @@ -1,17 +1,17 @@ - net7.0 + net10.0 enable false - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive