覆盖IEnumerable <Object> C#的Equals和GetHashCode

我正在研究类似的例子。 我正在尝试为实现PosInfo类列表的ListOfPosInfo覆盖Equals和GetHashCode。

class PosInfo
{
    public PosInfo(obj1 line, obj2 text)
    {
       _line = line;
       _text = text;
    }

    private obj1 _line;
    private obj2 text;

    public override bool Equals(object obj)
    {
        var equals = false;
        if (obj is PosInfo r)
        {
            equals = true;
            if (
                !line.Equals(r.line) || !text.Equals(r.text) 
                )
            {
                equals = false;
            }
        }
        return equals;
    }

    public override int GetHashCode()
    {
        return _line.GetHashCode() ^ _text.GetHashCode();
    }
}

class ListOfPosInfo 
{
    public ListOfPosInfo(IEnumerable<PosInfo> posInfos)
    {
        _posInfos = posInfos;
    }

    private IEnumerable<PosInfo> _posInfos;

    public override bool Equals(object obj)
    {
        return ....;
    }

    public override int GetHashCode()
    {
        return ....;
    }
}

如何正确覆盖ListPosInfo类的Equals和GetHashCode?

评论