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 143151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:03:00+00:00 2026-05-11T08:03:00+00:00

Below is a sample implementation of overriding Object.Equals() for an entity base class from

  • 0

Below is a sample implementation of overriding Object.Equals() for an entity base class from which all other entities in an application derive.

All entity classes have the property Id, which is a nullable int. (It’s the primary key of whatever table the entity class corresponds to.)

public override bool Equals(object obj)         {             if (obj == null || GetType() != obj.GetType())                 return false;              if (base.Equals(obj))                 return true;              return Id.HasValue && ((EntityBase) obj).Id.HasValue &&                    Id.Value == ((EntityBase) obj).Id.Value;         } 

Given this implementation of Equals(), how do you correctly implement GetHashCode()?

  • 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-11T08:03:01+00:00Added an answer on May 11, 2026 at 8:03 am

    If you’re deriving from something that already overrides GetHashCode I’d implement it as:

    public override int GetHashCode() {     unchecked     {         int hash = 37;         hash = hash * 23 + base.GetHashCode();         hash = hash * 23 + Id.GetHashCode();         return hash;     } } 

    A null value of Id will return 0 for Id.GetHashCode().

    If your class just derives from Object, I’d just return Id.GetHashCode() – you do not want to include the object.GetHashCode implementation in your hash code, as that basically ends up being object identity.

    Note that your equality definition won’t return true if neither entity has an Id, but the same hashcode will be returned from both objects. You may wish to consider changing your Equals implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below code sample, what does {0:X2} mean? This is from the reflection
Below is sample code to create a radio button element with Yes/No options in
I've been learning C#, and I'm trying to understand lambdas. In this sample below,
The code below shows a sample that I've used recently to explain the different
edit: VCL has no problem around right dragging and the sample program below works
The code below generates a warning CS3006 Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref
so I have a handler below: class PublishHandler(BaseHandler): def post(self): message = self.get_argument(message) some_function(message)
I am using Hibernate 3 as my persistence framework. Below is the sample hbm
When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the
I have a class for an RDT Header that holds information for an implementation

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.