我有一个struct
调用MyType
,实现IEquatable<MyType>
。我应该重写==对象和MyType吗?
我实施了operator ==(MyType x, MyType y)
,但是我是否还应该在下面的情况下执行operator ==(MyType x, object y)
?
例如:
public static bool operator ==(MyType x, object y)
{
if (y is MyType)
{
return x == (MyType)y;
}
return false;
}
用法:
var a = new MyType();
object b = new MyType();
var result = (a == b); // ?
你可以添加一个如何使用这个操作符的例子吗? – HimBromBeere
@HimBromBeere新增 – sdgfsdh
@HimBromBeere - wouldnt一个'as' cast从不返回'null',因为它的'struct'? – Igor