gpt4 book ai didi

c# - 理解 IEquatable

转载 作者:IT王子 更新时间:2023-10-29 03:48:55 25 4
gpt4 key购买 nike

当我使用 IEquatable<T> interface 实现要比较的对象时:

  1. 为什么我必须覆盖 Equals(object)方法,如果我已经实现 Equals(T)
  2. 我可以使用==吗?和 !=运营商一旦我实现 IEquatable<T>

最佳答案

  1. 来自 MS Docs article on IEquatable<T> :

    If you implement IEquatable<T>, you should also override the base class implementations of Equals(Object) and GetHashCode() so that their behavior is consistent with that of the Equals(T) method. If you do override Equals(Object), your overridden implementation is also called in calls to the static Equals(Object, Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results.

  2. 不,operators do not use the Equals method .它们必须是 overloaded separately这样做。

关于c# - 理解 IEquatable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/411500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com