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

  • SEARCH
  • Home
  • 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 250373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:30:21+00:00 2026-05-11T21:30:21+00:00

My understanding is that you’re typically supposed to use xor with GetHashCode() to produce

  • 0

My understanding is that you’re typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here’s a simple example:

class Foo
{
    int m_a;
    int m_b;

    public int A
    {
        get { return m_a; }
        set { m_a = value; }
    }

    public int B
    {
        get { return m_b; }
        set { m_b = value; }
    }

    public Foo(int a, int b)
    {
        m_a = a;
        m_b = b;
    }

    public override int GetHashCode()
    {
        return A ^ B;
    }

    public override bool Equals(object obj)
    {
        return this.GetHashCode() == obj.GetHashCode();
    }
}

The idea being, I want to compare one instance of Foo to another based on the value of properties A and B. If Foo1.A == Foo2.A and Foo1.B == Foo2.B, then we have equality.

Here’s the problem:

Foo one = new Foo(1, 2);
Foo two = new Foo(2, 1);

if (one.Equals(two)) { ... }  // This is true!

These both produce a value of 3 for GetHashCode(), causing Equals() to return true. Obviously, this is a trivial example, and with only two properties I could simply compare the individual properties in the Equals() method. However, with a more complex class this would get out of hand quickly.

I know that sometimes it makes good sense to set the hash code only once, and always return the same value. However, for mutable objects where an evaluation of equality is necessary, I don’t think this is reasonable.

What’s the best way to handle property values that could easily be interchanged when implementing GetHashCode()?

See Also

What is the best algorithm for an overridden System.Object.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. Editorial Team
    Editorial Team
    2026-05-11T21:30:21+00:00Added an answer on May 11, 2026 at 9:30 pm

    First off – Do not implement Equals() only in terms of GetHashCode() – hashcodes will sometimes collide even when objects are not equal.

    The contract for GetHashCode() includes the following:

    • different hashcodes means that objects are definitely not equal
    • same hashcodes means objects might be equal (but possibly might not)

    Andrew Hare suggested I incorporate his answer:

    I would recommend that you read this solution (by our very own Jon Skeet, by the way) for a “better” way to calculate a hashcode.

    No, the above is relatively slow and
    doesn’t help a lot. Some people use
    XOR (eg a ^ b ^ c) but I prefer the
    kind of method shown in Josh Bloch’s
    “Effective Java”:

    public override int GetHashCode()
    {
        int hash = 23;
        hash = hash*37 + craneCounterweightID;
        hash = hash*37 + trailerID;
        hash = hash*37 + craneConfigurationTypeCode.GetHashCode();
        return hash;
    }
    

    The 23 and 37 are arbitrary numbers
    which are co-prime.

    The benefit of the above over the XOR
    method is that if you have a type
    which has two values which are
    frequently the same, XORing those
    values will always give the same
    result (0) whereas the above will
    differentiate between them unless
    you’re very unlucky.

    As mentioned in the above snippet, you might also want to look at Joshua Bloch’s book, Effective Java, which contains a nice treatment of the subject (the hashcode discussion applies to .NET as well).

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

Sidebar

Related Questions

It's my understanding that common wisdom says to only use exceptions for truly exceptional
It is to my understanding that one should use a forward-class declaration in the
I have the understanding that perm size is used to store meta data, that
My understanding is that both JAAS and SQL Server can be configured to use
It is my understanding that Fortran, when reading data from file, will skip lines
Its my understanding that the questions in StackOverflow has the following format http://stackoverflow.com/questions/{question-id}/{slug-made-from-question-title} So
It is my understanding that since type/class unification every value is of a type
I have the understanding that using data access routines directly from presentation code is
Its my understanding that by adding the ScaffoldColumn(false) annotation to an property in a
It is my understanding that every type (other than some primitives like int 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.