我刚刚将Visual Studio更新到最新的16.6.0 Preview 1.0版本,并使用了最新的.NET Core 3.1.2。我的项目已经启用了<Nullable>enable</Nullable>
。
似乎IEquatable<T>
已经发生了变化,现在它被定义为[AllowNull]
:
public interface IEquatable<T>
{
bool Equals([AllowNull] T other);
}
因此,我实现了
IEquatable<T>
接口的任何类都会显示警告: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.。最好的解决方法是什么?
我可以添加一个可空操作符
bool Equals(T? o)
,或者我可以添加 [AllowNull]
(引用 System.Diagnostics.CodeAnalysis
)。两种方法似乎都能消除警告,但我不确定哪种更好。另外,我现在打开了一个我并不想要的
null
参数。
Equals
方法添加[AllowNull]
。 - Julien CouvreurIEquatable<T>
。 - Pavel Anikhouski