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

  • Home
  • SEARCH
  • 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 9251823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:47:49+00:00 2026-06-18T10:47:49+00:00

I have a HashSet. Is there a method that can utilize the IEqualityComparer for

  • 0

I have a HashSet. Is there a method that can utilize the IEqualityComparer for retrieving items where you pass in an object that will satisfies the equals method defined in the IEqualityComparer?

This might explain it a bit more.

    public class Program
{
    public static void Main()
    {
        HashSet<Class1> set = new HashSet<Class1>(new Class1Comparer());
        set.Add( new Class1() { MyProperty1PK = 1, MyProperty2 = 1});
        set.Add( new Class1() { MyProperty1PK = 2, MyProperty2 = 2});

        if (set.Contains(new Class1() { MyProperty1PK = 1 }))
            Console.WriteLine("Contains the object");

        //is there a better way of doing this, using the comparer?  
        //      it clearly needs to use the comparer to determine if it's in the hash set.
        Class1 variable = set.Where(e => e.MyProperty1PK == 1).FirstOrDefault();

        if(variable != null)
            Console.WriteLine("Contains the object");
    }
}

class Class1
{
    public int MyProperty1PK { get; set; }
    public int MyProperty2 { get; set; }
}

class Class1Comparer : IEqualityComparer<Class1>
{
    public bool Equals(Class1 x, Class1 y)
    {
        return x.MyProperty1PK == y.MyProperty1PK;
    }

    public int GetHashCode(Class1 obj)
    {
        return obj.MyProperty1PK;
    }
}
  • 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-18T10:47:50+00:00Added an answer on June 18, 2026 at 10:47 am

    If you want to retrieve items based on a single property, you might want to use a Dictionary<T,U> instead of a hashset. You can then place the items within the dictionary, using MyProperty1PK as the key.

    Your query then becomes simple:

    Class1 variable;
    if (!dictionary.TryGetValue(1, out variable)
    {
      // class wasn't in dictionary
    }
    

    Given that you’re already storing using a comparer which only uses this value as the uniqueness criteria, there is really no disadvantage to just using that property as the key in a dictionary instead.

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

Sidebar

Related Questions

I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I'm almost finished with my project, although there is one thing that I can't
I know that the .Contains method on a hashset is fast. My question is
I'm getting some strange behavior from a listview/the getChildAt method. I have a HashSet,
I need to implement equals() method for an Item that may be put in
I have a HashSet that I created and this is what it contains. It
why linq's except extension method does not have Except<TSource> Method (IEnumerable<TSource>,HashSet<TSource>) overload? for example
I have been using Java's ConcurrentMap for a map that can be used from

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.