Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 128879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:42:18+00:00 2026-05-11T05:42:18+00:00

Say I have a Point2 class, and I want to implement the following Equals:

  • 0

Say I have a Point2 class, and I want to implement the following Equals:

public override bool Equals ( object obj )  public bool Equals ( Point2 obj ) 

This is from the Effective C# 3 book:

public override bool Equals ( object obj ) {     // STEP 1: Check for null     if ( obj == null )     {         return false;     }      // STEP 3: equivalent data types     if ( this.GetType ( ) != obj.GetType ( ) )     {         return false;     }     return Equals ( ( Point2 ) obj ); }  public bool Equals ( Point2 obj ) {     // STEP 1: Check for null if nullable (e.g., a reference type)     if ( obj == null )     {         return false;     }     // STEP 2: Check for ReferenceEquals if this is a reference type     if ( ReferenceEquals ( this, obj ) )     {         return true;     }     // STEP 4: Possibly check for equivalent hash codes     if ( this.GetHashCode ( ) != obj.GetHashCode ( ) )     {         return false;     }     // STEP 5: Check base.Equals if base overrides Equals()     System.Diagnostics.Debug.Assert (         base.GetType ( ) != typeof ( object ) );      if ( !base.Equals ( obj ) )     {         return false;     }      // STEP 6: Compare identifying fields for equality.     return ( ( this.X.Equals ( obj.X ) ) && ( this.Y.Equals ( obj.Y ) ) ); } 

Is that an overkill?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:42:19+00:00Added an answer on May 11, 2026 at 5:42 am

    Supporting equality with an inheritance hierarchy is tricky. You need to work out exactly what you mean. Do you really need inheritance here? If not – if Point2 derives directly from System.Object, and you can make it sealed, life becomes a bit easier. In that case I would use:

    public override bool Equals (object obj) {     return Equals(obj as Point2); }  public bool Equals (Point2 obj) {     // STEP 1: Check for null if nullable (e.g., a reference type)     // Note use of ReferenceEquals in case you overload ==.     if (object.ReferenceEquals(obj, null))     {         return false;     }      // STEP 2: Check for ReferenceEquals if this is a reference type     // Skip this or not? With only two fields to check, it's probably     // not worth it. If the later checks are costly, it could be.     if (object.ReferenceEquals( this, obj))     {         return true;     }      // STEP 4: Possibly check for equivalent hash codes     // Skipped in this case: would be *less* efficient      // STEP 5: Check base.Equals if base overrides Equals()     // Skipped in this case      // STEP 6: Compare identifying fields for equality.     // In this case I'm using == instead of Equals for brevity     // - assuming X and Y are of a type which overloads ==.     return this.X == obj.X && this.Y == obj.Y; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have a class like this : public class MyClass { public
Let's say I have the following class X where I want to return access
Let's say I have a class Point: class Point { int x, y; public:
Let's say you have a class class C { int * i; public: C(int
Let's say I have two EJBs A and B: public class A implements AInterface
Let's say you have some Java code as follows: public class Base{ public void
Let's say I have an ActiveRecord called Apples, and I want a class method
Say I have public class family { public person father; public person mother; public
Say that I have an immutable Point class with x and y parameters, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.