C# IComparer标准用法问题

发布于 2024-09-10 23:42:41 字数 746 浏览 6 评论 0原文

我有一个问题,这是否是在 C# 中使用 IComparer 的标准。假设我有一个情况,其中有三个 Person 对象:P1、P2 和 P3。假设我调用 Compare 方法并传入 P1 和 P2,结果为 0。这本质上意味着两个人应该被归类为相等。现在假设我调用传入 P2 和 P3 的 Compare 方法,结果也是 0。同样,这意味着两个人是平等的。从逻辑上讲,可以假设P1和P3也相等;然而,Compare 方法可以被实现,但是有人决定实现它。那么在这种情况下 P1 和 P3 也返回 0 的方式实现它是一个标准吗?

这是我要问的代码:

// Assume these are initialized properly
Person p1 = null, p2 = null, p3 = null;
IComparer<Person> comparer = null;

// Compare person 1 to person 2 and result is 0
Debug.Assert(comparer.Compare(p1, p2) == 0);

// Compare person 2 to person 3 and result is 0
Debug.Assert(comparer.Compare(p2, p3) == 0);

// Would this be a fair assumption that person 1 and person 3 would also be 0?
Debug.Assert(comparer.Compare(p1, p3) == 0);

I have a question with whether or not this is a standard for using IComparer in C#. Say I have a situation in which there are three Person objects: P1, P2, and P3. Say I call the Compare method passing in P1 and P2 and the result is 0. This essentially means the two people should be categorized as equal. Now say I call the Compare method passing in P2 and P3 and the result for that is 0 as well. Again, this means the two people are equal. Logically speaking, one can assume P1 and P3 are equal as well; however, the Compare method could be implemented however someone decides to implement it. So is it a standard to implement it in such a way that P1 and P3 would also return 0 in this case?

Here's code of what I'm asking:

// Assume these are initialized properly
Person p1 = null, p2 = null, p3 = null;
IComparer<Person> comparer = null;

// Compare person 1 to person 2 and result is 0
Debug.Assert(comparer.Compare(p1, p2) == 0);

// Compare person 2 to person 3 and result is 0
Debug.Assert(comparer.Compare(p2, p3) == 0);

// Would this be a fair assumption that person 1 and person 3 would also be 0?
Debug.Assert(comparer.Compare(p1, p3) == 0);

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

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

发布评论

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

评论(4

挽容 2024-09-17 23:42:41

它与 C# 无关,它是一个简单的数学规则:传递性:
http://en.wikipedia.org/wiki/Transitive_relation

所以,简而言之,是的。

--- 由于评论而添加了信息 ---

如果您去阅读有关 IComparer 的文档:
http://msdn.microsoft.com/en- us/library/system.collections.icomparer.compare.aspx

你会看到:

Compares two objects and returns a value indicating whether one is less than, 
equal to, or greater than the other.

换句话说,这意味着当比较对象“a”和“b”时,调用时你应该总是得到相同的结果多次比较方法。如果不是这种情况,则意味着您将得到未定义的行为,并且不可能依赖该函数进行任何排序或比较。

因此,当您正确实现此方法时,传递性规则适用,您可以毫无疑问地说 a == c。

我希望澄清您对实施问题的疑问。

It has nothing to do with C#, it's a simple mathematical rule: Transitivity:
http://en.wikipedia.org/wiki/Transitive_relation

So, yes in short.

--- Added information due to comment ---

If you go and read the documentation about IComparer:
http://msdn.microsoft.com/en-us/library/system.collections.icomparer.compare.aspx

you'll see that:

Compares two objects and returns a value indicating whether one is less than, 
equal to, or greater than the other.

In other word, that means that when comparing object "a" and "b", you should always get the same result when calling multiple time the method Compare. If it's not the case, it means that you will get undefined behavior and it will be impossible to rely on that function for doing any sorting or comparison.

So, when you implement this method properly, the Transitivity rule apply and you can say without any doubt that a == c.

I hope that clarify your questioning about the implementation problem.

惟欲睡 2024-09-17 23:42:41

是的,这将是标准。它明确规定了 IComparable:

如果 A.CompareTo(B) 返回零并且
B.CompareTo(C) 返回零,然后
A.CompareTo(C)需要返回
零。

我在官方文档中找不到任何内容,并且对 ICompare 陈述了相同的内容,但我认为可以安全地假设同样的情况成立。

Yes, that would be the standard. Its explicitly stated for IComparable:

If A.CompareTo(B) returns zero and
B.CompareTo(C) returns zero, then
A.CompareTo(C) is required to return
zero.

I can't find anything in the official documentation that comes right out and states the same thing for ICompare but I think its safe to assume the same holds true.

黑色毁心梦 2024-09-17 23:42:41

这是接口契约的一部分,如果a == b并且b == ca == c(相等的传递属性) 。这在代码中的任何地方都没有强制执行,但它是正确运行所必需的。

It is part of the interface contract that if a == b and b == c that a == c (transitive property of equality). This is not enforced anywhere in code, but it is required for it to function correctly.

长发绾君心 2024-09-17 23:42:41

平等是传递性的,所以是的,您应该假设这一点,并在开发您的 IComparer 时牢记这一点。

传递性

Equality is transitive, so yes you should assume that, and develop your IComparer with that in mind.

Transitivity

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