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