dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.21k stars 5.86k forks source link

Implementation of Value Object abstract class raises errors with <nullable>enabled</nullable> #35441

Open KillerBoogie opened 1 year ago

KillerBoogie commented 1 year ago

The abstract class Value Object in the section value object implementation-in c shows 2 errors and two hints when <nullable> is enabled, which is the default setting for new projects.

1) The method public override bool Equals(object obj) raises error CS8765: "Nullability of type prameter 'obj' doesn't match doesn't match overriden member."

correct: public override bool Equals(object? obj)

2) The last line of EqualOperator method raises on ... left.Equals(right); error CS8602: "Dereference of possible null reference." First, the methods signature needs to be protected static bool EqualOperator(ValueObject? left, ValueObject? right) Second, the first line is an exclusive OR and referenceEquals is true if both parameters are null.Therefore, left can't be null and left!.Equals(right) will work.

3) ReferenceEquals(left, null) and ReferenceEquals(right, null) raises IDE0041: "Null check can be simplified" to left is null ^ right is null

Please update the documentation to reflect <nullable>enabled</nullable> setting!


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

jamesmontemagno commented 1 year ago

@erjain