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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:37:15+00:00 2026-05-25T22:37:15+00:00

Possible Duplicate: Create a hashcode of two numbers I have following class: public struct

  • 0

Possible Duplicate:
Create a hashcode of two numbers

I have following class:

public struct Test
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public override int GetHashCode()
    {
        return Prop1.GetHashCode() ^ Prop2.GetHashCode();
    }
}

Today I found out that I’m computing GetHashCode in a wrong way – if both properties are the same it always returns 0. How to do it the right way?

  • 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-25T22:37:16+00:00Added an answer on May 25, 2026 at 10:37 pm

    There is no single correct way to implement GetHashCode. Since your properties are strings there are an infinate number of possible combinations of values. The hash code is an Int32 so there only 2^32 possible values. Therefore, the HashCode cannot be unique for every possible value of Prop1 & Prop2. You have discovered one point where the value repeats and that is where Prop1 = Prop2. The idea of the hash code is that you get a fairly even distribution of values based upon the data you are expecting. If you exepect prop1 and prop2 to rarley be equal during execution, then its probably not an issue. However, if you expect prop1 and prop2 to be equal most of the time, then you should probably use a different algorithim for the hash code.

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

Sidebar

Related Questions

Possible Duplicate: Create new class from a Variable in Java I have a string
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: How do I create a HashCode in .net (c#) for a string
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: Can I create an automatic property (no private member) with get and
Possible Duplicate: Create WPF TextBox that accepts only numbers How i can check if
Possible Duplicate: How to create comma separated list from array in PHP? I have
Possible Duplicate: How can I create a UIColor from a hex string? I want
Possible Duplicate: Merging .net managed dlls I create a test project in which I
Possible Duplicate: Create a usercontrol instance programmatically in ASP.NET Dear Friend i have the

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.