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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:31:57+00:00 2026-06-17T11:31:57+00:00

That is what I have: class A { // The uniqueness of instance must

  • 0

That is what I have:

class A
{
  // The uniqueness of instance must be detected by this property
  public string Key { get; set; }

  // There are other properties
}

var set = HashSet<A>()

My general purposes are:

  • to provide identity of instances in the set collection by Key property value

  • to make this collection work as fast as possible for Contain operation

Answer to the following questions might help me to meet this purposes:

  1. What is used to run methods like Contains and Add which have to determine uniqueness of the instance: GetHashCode() or IEquatable? Most probably GetHashCode() as HashSet declared to be very fast for search.
  2. Default String.GetHashCode() implementation does not guarantee uniqueness of hash for 2 different string, so how can I provide uniqueness with performance in mind?
  3. Is IEquatable is used at all by HashSet?

Note his collection is created and destroyed in run-time only, it is not saved into Database

  • 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-17T11:31:58+00:00Added an answer on June 17, 2026 at 11:31 am

    Collections usually use Object.GetHashCode() and Object.Equals() to obtain hash codes and check for equality. There is no way to make Object.GetHashCode() return a unique hash code for all but the simplest objects – the hash code is only 32 bit wide and every object with more than 32 bits of internal state can not be mapped to unique hash codes. Therefore Object.Equals() is used to check for exact equality in case of hash code collisions.

    In consequence you have to override both mentioned methods with a suitable implementation.

    public override Int32 GetHashCode()
    {
        // If this.Key may be null you have to handle this case.
        return this.Key.HashCode();
    }
    
    public override Boolean Equals(Object obj)
    {
        var other = obj as A;
    
        return (other != null) && (this.Key == other.Key);
    }
    

    Alternatively you can use the HashSet<T> constructor accepting IEqualityComparer<T> and externalize both methods, for example if you have no control over the source code of the types you want to add to a set. Just create a class implementing the interface with suitable methods and pass an instance of this class to the HashSet<T> constructor.

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

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have a class that looks like this: class SearchService include Mongoid::Document key :name,
I have this class that have a function to load other classes and create
I have a validation that looks like this: class Book < ActiveRecord::Base belongs_to :author
We have this class here: public static class Helper { private static readonly Random
I currently have an algorithm that hashes a key and checks for it's uniqueness
How to select next n hidden elements that have class foo using jQuery?
Let's say that I have class , that uses some functionality of dict .
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that represents users. Users are divided into two groups with different

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.