How to best implement Equals for custom types?

前端 未结 10 726
春和景丽
春和景丽 2020-11-28 08:41

Say for a Point2 class, and the following Equals:

public override bool Equals ( object obj )

public bool Equals ( Point2 obj )

This is the

10条回答
  •  一个人的身影
    2020-11-28 09:20

    Lie Daniel L said,

    public override bool Equals(object obj) {
        Point2 point = obj as Point2; // Point2? if Point2 is a struct
        return point != null && this.Equals(point);
    }
    
    public bool Equals(Point2 point) {
        ...
    }
    

提交回复
热议问题