当我比较一个对象(类型)时,它是否使用特定类的 IEquatable?

发布于 2024-10-14 16:18:35 字数 268 浏览 4 评论 0原文

我的方法接收两个参数,均为 Object 类型。它们具有相同的类型,均实现 IEquatable

我的问题是:当我这样做时: param1 == param2 框架是否使用特定类的 IEquatable 运算符覆盖进行比较,还是使用 object.Equals< /code> 只是比较两个对象的内存指针?

哪种方法最好?是否具有泛型和派生约束?

My method receives two parameters, both of Object type. They have the same type, that implements IEquatable.

My question is: when I do: param1 == param2 does the framework compare using the IEquatable operator override of specific class or does it uses the object.Equals that just compares the memory pointer of two objects?

Which is the best way to do this? Is with generics and derivation constraints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-10-21 16:18:35

事实上,两者都没有。默认情况下,== 运算符将测试引用相等性,无论您的 Equals 方法的重写行为如何(如果您已重写它,那么您当然应该拥有它)实现了 IEquatable)。

也就是说,如果您的变量类型为 object 但您想使用自己的自定义相等比较,请使用 Equals(x, y) 而不是 x == y

然后,即使您已经实现了 IEquatable,也请务必仍然重写 object.Equals,如下所示:

class MyType : IEquatable<MyType>
{
    public bool Equals(MyType other)
    {
        // Whatever you want.
    }

    public override bool Equals(object other)
    {
        // Presumably you check for null above.
        return Equals(other as MyType);
    }
}

虽然您确实 < em>还可以为您的类型重载 ==!= 运算符,如果您引用了该类型的对象,那么这不会完成任何事情只是 object 变量,如下所示:

object x = new MyType();
object y = new MyType();
Console.WriteLine(Equals(x, y));
Console.WriteLine(x == y);

上面的内容不会像您预期的那样工作(如果您已经重载了 == 并希望使用它),因为 < code>== 重载必须在编译时解决;由于 xy 被输入为任意对象,C# 编译器将选择 object 类型的 == 运算符,同样,它只是测试引用相等性。


更新:现在,如果您的变量被键入为定义它的类,您可以确保使用==运算符或者 更派生的类型。例如,给定以下类型:

class A
{
    public static bool operator ==(A x, A y) { return true; }
    public static bool operator !=(A x, A b) { return false; }
}

class B : A { }

class AComparer<T> where T : A
{
    public bool CompareEqual(T x, T y) { return x == y; }
}

上面的 AComparer.CompareEqual 方法将使用重载的 == 运算符来处理任何类型T 源自 A

要记住的关键一点是 ==静态,这意味着它的重载解析是在编译时执行的,而不是在运行时使用 vtable 执行的(除非您'正在使用dynamic,但这完全是另一回事)。因此,每当您在代码中使用 == 运算符并且希望重载解析为您的自定义类型时,请注意这一点。

Actually, it does neither. The == operator by default will test for reference equality, regardless of the overridden behavior of your Equals method (if you've overridden it, which you certainly should have if you implemented IEquatable<T>).

That is to say, if your variables are typed as object but you want to use your own custom equality comparison, use Equals(x, y) rather than x == y.

Then, even if you've implemented IEquatable<T>, be sure to still override object.Equals, like this:

class MyType : IEquatable<MyType>
{
    public bool Equals(MyType other)
    {
        // Whatever you want.
    }

    public override bool Equals(object other)
    {
        // Presumably you check for null above.
        return Equals(other as MyType);
    }
}

While you certainly can also overload the == and != operators for your type, this won't accomplish anything if you have references to objects of this type that are simply object variables, like this:

object x = new MyType();
object y = new MyType();
Console.WriteLine(Equals(x, y));
Console.WriteLine(x == y);

The above won't work as you might expect (if you've overloaded == and expect that to be used) because the == overload has to be resolved at compile-time; since x and y are typed as arbitrary objects, the C# compiler will pick the object type's == operator, which, again, just tests for reference equality.


Update: Now, you can ensure your == operator is used if your variables are typed as the class wherein you defined it or a more derived type. For example, given the following types:

class A
{
    public static bool operator ==(A x, A y) { return true; }
    public static bool operator !=(A x, A b) { return false; }
}

class B : A { }

class AComparer<T> where T : A
{
    public bool CompareEqual(T x, T y) { return x == y; }
}

The AComparer<T>.CompareEqual method above will use your overloaded == operator for any type T deriving from A.

The key thing to remember is that == is static, which means its overload resolution gets performed at compile-time, not at run-time using a vtable (unless you're using dynamic, but that's a whole other beast). So just be aware of that whenever you're using the == operator in code and you want the overload to resolve to that of your custom type.

如果您的方法的参数指定为 object ,则执行 param1 == param2 仅执行引用相等,因为 == 运算符不是多态的。

If your method's parameters are specified as object then doing param1 == param2 just performs reference equality, since the == operator isn't polymorphic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文