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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:46:06+00:00 2026-05-13T10:46:06+00:00

I have a pretty complex object and I need to get uniqueness of these

  • 0

I have a pretty complex object and I need to get uniqueness of these objects. One solution can be done by overriding GetHashCode(). I have implemented a code noted below:

public override int GetHashCode()
{
    return this._complexObject1.GetHashCode() ^
           this._complexObject2.GetHashCode() ^
           this._complexObject3.GetHashCode() ^
           this._complexObject4.GetHashCode() ^
           this._complexObject5.GetHashCode() ^
           this._complexObject6.GetHashCode() ^
           this._complexObject7.GetHashCode() ^
           this._complexObject8.GetHashCode();
}

These complex objects also overrides GetHashCode() and does similar operations.

My project requires uniqueness of these objects which I deal with these very frequently, and data inside also changes in various ways and places.

I need a faster way to find uniqueness of these complex objects, which need to consider performance and memory.

Thanks in advance
Munim

  • 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-13T10:46:06+00:00Added an answer on May 13, 2026 at 10:46 am

    Given your comment, it sounds like you may be trying to rely on GetHashCode on its own to determine uniqueness. Don’t do that. Hashes aren’t meant to be unique – it’s meant to be unlikely that two unequal objects will hash to the same value, but not impossible. If you’re trying to check that a set of objects has no duplicates, you will have to use Equals as well.

    Note that using XOR for a hashcode can make it more likely that you’ll get hash collisions, depending on the individual hash values involved. In particular, it makes any two equal fields “cancel each other out”. I generally use this form:

    int hash = 17;
    hash = hash * 31 + field1.GetHashCode();
    hash = hash * 31 + field2.GetHashCode();
    hash = hash * 31 + field3.GetHashCode();
    hash = hash * 31 + field4.GetHashCode();
    ...
    return hash;
    

    … but even so, that’s certainly not going to guarantee uniqueness. You should use GetHashCode() to rule out equality, and then use Equals to check the actual equality of any potentially equal values.

    Now your question mentions speed – this sounds like the perfect place to use a profiler and some benchmark tests. Are you sure this is a bottleneck? If you have many different types all computing hash values, have you found out which of these is the biggest contributor to the problem?

    Some optimisations will depend on exactly how you use the data. If you find that a lot of your time is spent recomputing hashes for values which you know haven’t changed, you could cache the hash code… although this obviously becomes trickier when there are fields which themselves refer to complex objects. It’s possible that you could cache “leaf node” hashes, particularly if those leaf nodes don’t change often (but their usage could vary).

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

Sidebar

Related Questions

I have a class that represents a pretty complex object. The objects can be
I have a pretty complex object graph G with an object o1 in G
I have a pretty complex linq statement I need to access for different methods.
I've got a pretty complex object graph that I want to load in one
I have an app running on iPAD. Pretty complex one I would say. The
I have a pretty complex database schema and would like to know if there
I have a pretty complex criteria, which I use to retrieve, sort and page
I have built a PHP calendar system and have pretty much everything done but
I have a pretty complex form with lots of inputs and validators. For the
I have a pretty complex chat application going on, and there are multiple chat

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.