Equals method of System.Collections.Generic.List<T>…?

旧街凉风 提交于 2019-12-01 15:26:08

You can use SequenceEqual linq method on the list since your list implements IEnumerable. This will verify all the elements are the same and in the same order. If the order may be different, you could sort the lists first.

public class MyList<T> : List<T>
{
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        MyList<T> list = obj as MyList<T>;
        if (list == null)
            return false;

        if (list.Count != this.Count)
            return false;

        bool same = true;
        this.ForEach(thisItem =>
        {
            if (same)
            {
                same = (null != list.FirstOrDefault(item => item.Equals(thisItem)));
            }
        });

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