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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:34:40+00:00 2026-06-05T05:34:40+00:00

A HashSet<T> can determine in O(1) whether it contains a certain item. If I

  • 0

A HashSet<T> can determine in O(1) whether it contains a certain item. If I override Equals() and GetHashCode() on my custom class, I can have an object A and another object A’ that are not equal by identity but for which Equals() returns true and GetHashCode() returns the same hash code.

Now, given that A is in the hash set, I want to retrieve A in O(1) given A’ (which is equal to A from the perspective of the hash set).

var a = new MyClass("A");
var a_prime = new MyClass("A");
Debug.Assert(a.Equals(a_prime));
Debug.Assert(a.GetHashCode() == a_prime.GetHashCode());

var set = new HashSet<MyClass>();
set.Add(a);
Debug.Assert(set.Contains(a_prime));

// This:    
var retrieved_a = set.Get(a_prime);

How to do this?

(Note that this has not the answer I’m looking for, and this has no answers at all.)


Some background information: I want to use the set to intern my own objects the same way C# interns strings: equal objects need only one instance. This way I can append metadata to such an object and be sure that there is no other equal instance anywhere without that metadata.

  • 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-05T05:34:42+00:00Added an answer on June 5, 2026 at 5:34 am

    There is no method on HashSet that does what you want.

    You can use a Dictionary instead:

    var dict = new Dictionary<MyClass, MyClass>();
    dict[a] = a;
    Debug.Assert(dict.ContainsKey(a_prime));
    var retrieved_a = dict[a_prime];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anybody tell me how I can override HashSet's contains() method to use a
I have developed one application which can using hashset concept. I have to override
I know that the HashSet.contains() method uses the .equals method to check equality as
Good afternoon In Java, I have HashSet which contain list of Object User which
I have a HashSet that contains multiple lists of integers - i.e. HashSet<List<int>> In
According to the HashSet javadoc, HashSet.contains only returns a boolean. How can I find
I have a set of 100 object. How can i get a subset of
I have a static HashSet of object references in my code, which has to
How can one sort a HashSet<string> in c# .Net 3.5 ?
Can someone explain what the following does? private HashSet nodes[]; nodes = new HashSet[21];

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.