修改List.Contains行为 [英] Modify List.Contains behavior

查看:235
本文介绍了修改List.Contains行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表与LT; MyObj中> 类MyObj中:IComparable的。我写在 MyObj中每类 IComparable的的CompareTo >接口,但是当我使用列表< MyObj中>。载有(myObjInstance)返回当它应该是真正

I have a List<MyObj> with the class MyObj : IComparable. I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true.

我不知道我的理解我需要如何进行,以确保列表使用我的自定义比较方法然后调用时包含函数

I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison method when calling then Contains function.

下面是我的compareTo实现:

Here is my compareTo implementation:

    #region IComparable Members

    public int CompareTo(object obj)
    {
        MyObj myObj = (MyObj)obj;
        return String.Compare(this.Symbol, myObj.Symbol, true);
    }

    #endregion

请注意符号属性是字符串。

Note the Symbol property is a string.

要澄清,我已经把一个停止点在compareTo方法,它甚至不进去。

To clarify I've put a stopping point in that compareTo method and it doesn't even go in there.

任何人曾经尝试过?

感谢。

推荐答案

要找出是否你的CompareTo方法称为绝对最简单的方法是设置中有一个断点,按F5运行程序。但我相信,列表< T>。载查找的 IEquatable< T> 接口,用来比较

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.

这篇关于修改List.Contains行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆