I just updated Visual Studio to latest 16.6.0 Preview 1.0 with latest .NET Core 3.1.2. My project has <Nullable>enable</Nullable>
.
It seems there has been a change to IEquatable<T>
and now it is defined with [AllowNull]
:
public interface IEquatable<T>
{
bool Equals([AllowNull] T other);
}
As a result, any class of mine which implemented IEquatable<T>
with bool Equals(T o)
now shows a warning:
CS8767
: Nullability of reference types in type of parameter 'o' of 'bool MyType.Equals(MyType o)' doesn't match implicitly implemented member 'bool IEquatable.Equals(MyType other)' because of nullability attributes.
What is the best way to solve this ?
I can add a nullable operator bool Equals(T? o)
, or I can add [AllowNull]
(with reference to System.Diagnostics.CodeAnalysis
). Both seem to make the warning go away, but I'm not sure which one is better.
Also, I now opened the door for a null
parameter which I didn't really want.
[AllowNull]
to yourEquals
method – Julien CouvreurIEquatable<T>
– Pavel Anikhouski