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.
ID: 70b14bbb-dc66-f1a2-8b14-39ef3b4bde1d
Version Independent ID: cf912666-9ad3-1673-8f69-6bf52c034ef9
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 beprotected static bool EqualOperator(ValueObject? left, ValueObject? right)
Second, the first line is an exclusive OR and referenceEquals is true if both parameters arenull.
Therefore,left
can't benull
andleft!.Equals(right)
will work.3)
ReferenceEquals(left, null)
andReferenceEquals(right, null)
raises IDE0041: "Null check can be simplified" toleft 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.