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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:25:17+00:00 2026-05-11T19:25:17+00:00

I have an Address class in C# that looks like this: public class Address

  • 0

I have an Address class in C# that looks like this:

public class Address
{            
    public string StreetAddress { get; set; }
    public string RuralRoute { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
}

I’m implementing equality and so I need to override the hash code. At first I was going to use the hashcode formula from EJ but then I thought: These are all string fields, can’t I just just use a StringBuilder to concatenate them and return the hash code from that string?

That is:

var str = new StringBuilder();
str.Append(StreetAddress)
   .Append(RuralRoute)
   ...

return str.ToString().GetHashCode();

What are the advantages/disadvantages of this? Why shouldn’t I do it?

  • 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-11T19:25:18+00:00Added an answer on May 11, 2026 at 7:25 pm

    I would avoid doing that simply on the grounds that it creates a bunch of strings pointlessly – although Kosi2801’s point about making collisions simple is also relevant. (I suspect it wouldn’t actually create many collisions, due to the nature of the fields, but…)

    I would go for the “simple and easy to get right” algorithm I’ve previously used in this answer (thanks for looking it up lance 🙂 – and which is listed in Effective Java, as you said. In this case it would end up as:

    public int GetHashCode()
    {
        int hash = 17;
        // Suitable nullity checks etc, of course :)
        hash = hash * 23 + StreetAddress.GetHashCode();
        hash = hash * 23 + RuralRoute.GetHashCode();
        hash = hash * 23 + City.GetHashCode();
        hash = hash * 23 + Province.GetHashCode();
        hash = hash * 23 + Country.GetHashCode();
        hash = hash * 23 + PostalCode.GetHashCode();
        return hash;
    }
    

    That’s not null-safe, of course. If you’re using C# 3 you might want to consider an extension method:

    public static int GetNullSafeHashCode<T>(this T value) where T : class
    {
        return value == null ? 1 : value.GetHashCode();
    }
    

    Then you can use:

    public int GetHashCode()
    {
        int hash = 17;
        // Suitable nullity checks etc, of course :)
        hash = hash * 23 + StreetAddress.GetNullSafeHashCode();
        hash = hash * 23 + RuralRoute.GetNullSafeHashCode();
        hash = hash * 23 + City.GetNullSafeHashCode();
        hash = hash * 23 + Province.GetNullSafeHashCode();
        hash = hash * 23 + Country.GetNullSafeHashCode();
        hash = hash * 23 + PostalCode.GetNullSafeHashCode();
        return hash;
    }
    

    You could create a parameter array method utility to make this even simpler:

    public static int GetHashCode(params object[] values)
    {
        int hash = 17;
        foreach (object value in values)
        {
            hash = hash * 23 + value.GetNullSafeHashCode();
        }
        return hash;
    }
    

    and call it with:

    public int GetHashCode()
    {
        return HashHelpers.GetHashCode(StreetAddress, RuralRoute, City,
                                       Province, Country, PostalCode);
    }
    

    In most types there are primitives involved, so that would perform boxing somewhat unnecessarily, but in this case you’d only have references. Of course, you’d end up creating an array unnecessarily, but you know what they say about premature optimization…

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

Sidebar

Related Questions

I have the following: public class Address { public string Email { get; set;
Let's say we have a class that looks like this: class A { public:
I have a class structure that looks like this: class Person { public virtual
I have a class that looks like this: public class Person { public class
Ok, I have a ViewModel that looks like this: public class UserLogin { [Required]
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
I have a class user which looks like this: public class User { public
I'm perplexed by this. I have a login action that looks like this: public
I have a class with information about a Person that looks something like this:
Say I have a simple address class like below: public class Address { public

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.