IEqualityComparer.Equals 与 IEnumerable.Contains 一起使用时,x 或 y 是列表中的值吗?
IEnumberable 有一个扩展方法 Contains
我的问题是 X 或 Y 是 IEnumerable 中的值?
示例
List<string> test = new List<String() { "a", "b", "c" };
test.Contains("d", myComparer);
当它调用第一个值的 Equals 方法时,它将是 等于(“a”,“d”)还是等于(“d”,“a”)?
IEnumberable has an extension method Contains<T> which takes two parameters. The first parameter is the value to check for and the second is an implementation of IEqualityComparer.
Looking at IEqualityComparer.Equals it takes two parameters named x and y, for the first and second objects to compare.
My question is X or Y the value from the IEnumerable?
Example
List<string> test = new List<String() { "a", "b", "c" };
test.Contains("d", myComparer);
When it calls to the Equals method for the first value will it be
Equals("a","d") or Equals("d","a")?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不重要——平等应该是对称的。 来自
IEqualityComparer.Equals
< 的文档/a>:我不相信
Enumerable.Contains
中的用法是明确定义的,即它可能会在未来版本中发生变化。 如果您只是让相等比较器遵守接口文档,那就没问题了。It shouldn't matter - equality should be symmetric. From the docs for
IEqualityComparer<T>.Equals
:I don't believe the usage in
Enumerable.Contains
is well-defined, i.e. it could change in a future version. If you just make your equality comparer obey the interface documentation, you'll be fine.为了完整起见,IEnumberable 的反射代码显示它位于左侧(见下文)。 然而,这并没有承诺永远不会改变,所以使用它是有风险的。
For the sake of completeness the reflected code of IEnumberable shows it is on the left hand side (see below). However that is not promised not to ever change, so there is risk in using it.