谢谢解决方案它将使用默认的相等比较器.如果一个对象能够与其他对象进行比较,那么它应该实现IEquatable,而不是IEqualityComparer.如果某个类型实现了IEquatable,则它将用作EqualityCOmparer.Default的实现,否则将使用object.Equals和object.GetHashCode方法. IEqualityComparer旨在比较 other 对象是否相等,而不是本身.Lets say I instantiate a dictionary like thisvar dictionary = new Dictionary<MyClass, SomeValue>();And MyClass is my own class that implements an IEqualityComparer<>. Now, when I do operations on the dictionary - such as Add, Contains, TryGetValue etc - does dictionary use the default EqualityComparer<T>.Default since I never passed one into the constructor or does it use the IEqualityComparer that MyClass implements?Thanks 解决方案 It will use the default equality comparer.If an object is capable of comparing itself for equality with other objects then it should implement IEquatable, not IEqualityComparer. If a type implements IEquatable then that will be used as the implementation of EqualityCOmparer.Default, followed by the object.Equals and object.GetHashCode methods otherwise.An IEqualityComparer is designed to compare other objects for equality, not itself. 这篇关于词典中使用哪个IEqualityComparer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 06:14