IEnumberable 有一个扩展方法 Contains
我的问题是 X 或 Y 是 IEnumerable 中的值?
示例
List<string> test = new List<String() { "a", "b", "c" };
test.Contains("d", myComparer);
当它调用第一个值的 Equals 方法时,它会是 等于(“a”,“d”)还是等于(“d”,“a”)?
最佳答案
这不重要——平等应该是对称的。来自 IEqualityComparer<T>.Equals
的文档:
The Equals method is reflexive, symmetric, and transitive. That is, it returns true if used to compare an object with itself; true for two objects x and y if it is true for y and x; and true for two objects x and z if it is true for x and y and also true for y and z.
我不相信 Enumerable.Contains
中的用法定义明确,即它可能在未来版本中更改。如果您只是让相等比较器遵守接口(interface)文档,那就没问题了。
关于.net - IEqualityComparer.Equals 与 IEnumerable.Contains 一起使用时,x 或 y 是列表中的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/751081/