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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:34:11+00:00 2026-06-08T18:34:11+00:00

When generating hashcodes for a class, is it ok to use the hashcodes of

  • 0

When generating hashcodes for a class, is it ok to use the hashcodes of that class’s members? Here is a sample class:

class Sample
{
    private readonly string _strA, _strB;
    public Sample(string strA, string strB)
    {
        this._strA = strA;
        this._strB = strB;
    }
    public override int GetHashCode()
    {
        return (this._strA + "###" + this._strB).GetHashCode();
    }
}

I think this will work as long as neither _strA nor _strB contain the string “###”. I’m not totally sure though as I don’t know the specifics of how hashcodes are generated on strings.

I saw a solution in the post at Create a hashcode of two numbers that I could tailor for my purposes, but I think that my solution is more simple (as long as neither string contains “###”).

  • 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-08T18:34:12+00:00Added an answer on June 8, 2026 at 6:34 pm

    A better approach would be to combine the hash codes mathematically, using something like the Times 33 hash. In your current code you create a temporary string each time GetHashCode is called, which could suffer from poor performance.

    public override int GetHashCode()
    {
        // omit null-coalesce if we know them to be non-null
        return (33 * (this._strA ?? "").GetHashCode())
             + (this._strB ?? "").GetHashCode();
    }
    

    If your class is truly immutable, calculation of the hashcode up front may be worth the 4-bytes:

    private readonly int _hash;
    
    public Sample(string strA, string strB)
    {
        this._strA = strA;
        this._strB = strB;
        this._hash = (33 * (this._strA ?? "").GetHashCode())
                   + (this._strB ?? "").GetHashCode();
    }
    
    public override int GetHashCode()
    {
        return this._hash;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Customer class. public class Customer { private string _id; private string
For generating random string i written code like this But if the function is
I'm generating code in java using SWIG. One of the generated class belongs to
Generating a random password is easy. but generating a batch is more difficult. public
When generating IL using DynamicMethod it's possible to call methods and access fields that
In generating a select tag for a Boolean value, I use the following code:
I am generating pdf using java and itext. When I add image that we
i m generating a pdf file from the contents of email and that email
I have the following class: public class SimplePolygon { Point[] pointCollection; public SimplePolygon(Point[] pointCollection)
Generating a scaffold like this: rails generate scaffold User name:string email:string creates bits of

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.