Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
{
public class Address
{
public string FirstName { get; set; }

Check warning on line 5 in csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning)

Non-nullable property 'FirstName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning)

Non-nullable property 'FirstName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

#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; }

Check warning on line 11 in csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning)

Non-nullable property 'Region' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier, or declaring the property as nullable, or safely handling the case where 'field' is null in the 'get' accessor.

Check warning on line 11 in csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning/NonNullablePropertyWarning/Address.cs

View workflow job for this annotation

GitHub Actions / Build & test (csharp-basic-topics/NonNullablePropertyMustContainANonNullValueWarning)

Non-nullable property 'Region' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier, or declaring the property as nullable, or safely handling the case where 'field' is null in the 'get' accessor.
#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; }
Expand All @@ -19,5 +22,11 @@
public string PostalCode { get; set; } = null!;

public required string Area { get; set; }

public string Town
{
get => field ?? string.Empty;
set => field = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NonNullablePropertyWarning;
using HelperAddress = NonNullablePropertyWarning.HelperMethod.Address;

namespace Tests
{
Expand All @@ -16,8 +17,8 @@ public void Setup()
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void WhenDereferencingFirstName_ThenExceptionThrown() => _ = _address?.FirstName.Length;
public void WhenDereferencingFirstName_ThenExceptionThrown() =>
Assert.ThrowsExactly<NullReferenceException>(() => _ = _address!.FirstName.Length);

[TestMethod]
public void WhenInstantiatingAddress_ThenFirstNameNull() => Assert.IsNull(_address?.FirstName);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading