This confuses me all the time, == Equals IEquatable ??
Found an interesting piece of code to help me remember that.
public class Point : IEquatable {
#region Implementation of equality
public bool Equals(Point other) {
if(ReferenceEquals(other, null))
return false;
// TODO Implement comparison logic here. E.g.:
// return X.Equals(other.X) && Y.Equals(other.Y);
throw new Exception("The method or operation is not implemented.");
}
public override bool Equals(object obj) {
return Equals(obj as Point);
}
public static bool operator ==(Point lhs, Point rhs) {
return ReferenceEquals(lhs, rhs)
!ReferenceEquals(lhs, null) && lhs.Equals(rhs);
}
public static bool operator !=(Point lhs, Point rhs) {
return !(lhs==rhs);
}
public override int GetHashCode() {
// TODO Implement comparison logic here. E.g.:
// return X.GetHashCode() ^ Y.GetHashCode();
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
** Refer Effective C# Programming for better understanding.
Found an interesting piece of code to help me remember that.
public class Point : IEquatable
#region Implementation of equality
public bool Equals(Point other) {
if(ReferenceEquals(other, null))
return false;
// TODO Implement comparison logic here. E.g.:
// return X.Equals(other.X) && Y.Equals(other.Y);
throw new Exception("The method or operation is not implemented.");
}
public override bool Equals(object obj) {
return Equals(obj as Point);
}
public static bool operator ==(Point lhs, Point rhs) {
return ReferenceEquals(lhs, rhs)
!ReferenceEquals(lhs, null) && lhs.Equals(rhs);
}
public static bool operator !=(Point lhs, Point rhs) {
return !(lhs==rhs);
}
public override int GetHashCode() {
// TODO Implement comparison logic here. E.g.:
// return X.GetHashCode() ^ Y.GetHashCode();
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
No comments:
Post a Comment