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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:45:28+00:00 2026-05-15T00:45:28+00:00

I want to know the best way to compare two objects and to find

  • 0

I want to know the best way to compare two objects and to find out if they’re equal. I’m overriding both GethashCode and Equals. So a basic class looks like:

public class Test
{
    public int Value { get; set; }
    public string String1 { get; set; }
    public string String2 { get; set; }

    public override int GetHashCode()
    {
        return Value ^ String1.GetHashCode() ^ String2.GetHashCode();
    }

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

So for testing purposes I created two objects:

Test t = new Test()
{
    Value = 1,
    String1 ="One",
    String2 = "One"
};

Test t2 = new Test()
{
    Value = 1,
    String1 = "Two",
    String2 = "Two"
};

bool areEqual = t.Equals( t2 );

In testing this areEqual returns true event though both objects are different. I realise this is because String1 and String2 are the same value in each object and thus cancels each other out when hashing.

Is there a better way off hashing object that the method I have that will resolve my issue?

  • 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-15T00:45:28+00:00Added an answer on May 15, 2026 at 12:45 am

    Your current equality method is broken – there are more values than possible hash codes. It’s entirely reasonable (and expected) that you will occasionally have values which are unequal but give the same hash. Equals should check the actual values:

    public override bool Equals(object obj)
    {
        Test test = obj as Test;
        if (obj == null)
        {
            return false;
        }
        return Value == test.Value &&
            String1 == test.String1 &&
            String2 == test.String2;
    }
    

    A few things to note:

    • Your way of generating the hashcode will give the same value for any fixed Value if String1 and String2 are the same; it will also blow up if String1 or String2 is null. This is an unfortunate aspect of using XOR for hashing. I prefer something like this:

      // Put this extension method in a utility class somewhere
      public static int SafeGetHashCode<T>(this T value) where T : class
      {
          return value == null ? 0 : value.GetHashCode();
      }
      
      // and this in your actual class
      public override int GetHashCode()
      {
          int hash = 19;
          hash = hash * 31 + Value;
          hash = hash * 31 + String1.SafeGetHashCode();
          hash = hash * 31 + String2.SafeGetHashCode();
          return hash;
      }
      
    • Generally speaking, equality becomes tricky when inheritance gets involved. You may want to consider sealing your class.

    • You may also want to implement IEquatable<Test>

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

Sidebar

Related Questions

I am developing an android app and I want to know the best way
I've been at this for a while and want to know the best way
I want to know the best way how to obtain the basic info of
Possible Duplicate: What is the best way to compare two entity framework entities? I
I want to know the best way of transferring a cryptographic initialization vector (IV)
I have to compare two lists and only know if they contain same values
I'm learning Scala and I want to know the best way of expressing this
I have a login in my application. I want to know the best way
I have a DateTime in Ticks. I want to know the best way to
I want to know the best way to implement this (approach). I will bee

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.