Modify List.Contains behavior

冷暖自知 提交于 2019-12-01 03:54:33

The absolute easiest way to find out whether your CompareTo method is called is to set a breakpoint in it and hit F5 to run your program. But I believe that List<T>.Contains looks for the IEquatable<T> interface for making the comparison.

According to the documentation for List<T>.Contains, it uses either your implementation of IEquatable interface or object.Equals, that you can override as well.

Did you try overriding the Equals method?

List<T>, according to reflector, uses EqualityComparer<T> to check for containment, and the default implementation (ObjectEqualityComparer) uses Equals for most normal objects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!