An article in the Microsoft MSDN library on the Object.Equals method (object), ( http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx ) provides an example that demonstrates how to override Equals. It looks like this:
class Point { ... // IEquatable<Point> is not implemented. public override bool Equals(Object obj) { //Check for null and compare run-time types. if ((obj == null) || ! this.GetType().Equals(obj.GetType())) { return false; } else { Point p = (Point) obj; return (x == px) && (y == py); } } } sealed class Point3D: Point { int z; public override bool Equals(Object obj) { Point3D pt3 = obj as Point3D; if (pt3 == null) return false; else return base.Equals((Point)obj) && z == pt3.z; // Here!!! } }
In the following documentation, my attention was drawn to the following statement.
(If it is a Point3D object, it is transformed into a Point object and passed to the implementation of the base class Equals .)
Here, return base.Equals((Point)obj) why worry about return base.Equals((Point)obj) obj to Point ?
Update:
I assume this may be a typo, as I am checking a .NET 4.0 version document, this is single-line:
return base.Equals(obj) && z == ((Point3D)obj).z
source share