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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:13:41+00:00 2026-06-05T15:13:41+00:00

Can I write a hash code function for the following comparer logic? Two instances

  • 0

Can I write a hash code function for the following comparer logic?

Two instances of My are equal if at least two properites from (A, B, C) match.

The Equals part is simple, but I’m stumped on the hash code part, and part of me is thinking it might not be possible.

class MyOtherComparer : IEqualityComparer<My>
{
    public bool Equals(My x, My y)
    {
        if (Object.ReferenceEquals(x, y)) 
            return true;      

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) 
            return false;

        int matches = 0;

        if(x.A == y.A) matches++;

        if(x.B == y.B) matches++;

        if(x.C == y.C) matches++;

        // match on two out of three
        return  (matches > 1)
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.
    public int GetHashCode(My x)
    {
       // ???
    }
}

UPDATE: In addition to the correct answer by Reed Copsey, a very important point about the general usefulness of a fuzzy comparer is clearly stated by Ethan Brown – please see his answer as well for a full understanding of what underlies this Question/Answer.

  • 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-06-05T15:13:42+00:00Added an answer on June 5, 2026 at 3:13 pm

    Yes, it’s possible. The simplest implementation would be to always return a constant.

    public int GetHashCode(My x) 
    { 
       return 0;
    }
    

    The GetHashCode documentation states:

    Implementations are required to ensure that if the Equals method returns true for two objects x and y, then the value returned by the GetHashCode method for x must equal the value returned for y.

    However, you are perfectly free to return the same hash code for two objects which are not equal, as well.

    That being said, this will potentially cause some algorithms to perform very poorly, as you’ll get lots of hash collisions. However, given the nature of your odd/unique equality check, this may be required.


    Note that this is going to be problematic in any case. Given your logic, it’s possible to have three objects, where comparer.Equals(foo, bar)==true and comparer.Equals(foo, baz)==true but comparer.Equals(baz, bar)==false. This is likely to be problematic in many cases where IEqualityComparer<T> is used.

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

Sidebar

Related Questions

Recently I refactored the code of a 3rd party hash function from C++ to
I would like to write a function GetHashCodeOfList() which returns a hash-code of a
I can write an assertion message one of two ways. Stating success: assertEquals( objects
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
I can write my code in both the ways to achieve my objective, but
How we can write a LINQ query for following select sql query: string brandid=1,2,3
In C++ one can write any of the following statements: 10; true; someConstant; //if
When i write the following code Post.find(:all,:group=>'comments') It only gives me the first record
I can access a python function's attribute inside of function itself by below code:
I want to write a little DBQuery function in perl so I can have

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.