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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:27:51+00:00 2026-05-15T08:27:51+00:00

I have a HashSet<MyCustomClass> mySet = new HashSet<MyCustomClass>(); and I wish to remove all

  • 0

I have a HashSet<MyCustomClass> mySet = new HashSet<MyCustomClass>(); and I wish to remove all MyCustomClass’s that contain the same values.

Let’s say MyCustomClass looks like this:

public class MyCustomClass
{
    Point point;

    public MyCustomClass(int x, int y)
    {
        point.X = x;
        point.Y = y;
    }

    // Other methods...
}

I tried to implement IEqualityComparer like MSDN suggests, and pass it through the constructor of the HashSet<MyCustomClass>(); but I ended up unsuccessfully.

What’s the correct approach?

EDIT:

This is my Chain class and my ChainEqualityComparer:

public class Chain
{
    HashSet<Mark> chain;
    HashSet<Mark> marks;

    public Chain(HashSet<Mark> marks)
    {
        chain = new HashSet<Mark>();
        this.marks = marks;
    }       
    // Other methods...
}

public class ChainEqualityComparer : IEqualityComparer<Chain>
{
    #region IEqualityComparer<Chain> Members

    public bool Equals(Chain x, Chain y)
    {
        if (x.ChainWithMarks.Count == y.ChainWithMarks.Count)
        {
            foreach (Mark mark in x.ChainWithMarks)
            {
                if (!y.ChainWithMarks.Contains(mark))
                    return false;
            }
            return true;
        }

        return false;
    }

    public int GetHashCode(Chain obj)
    {
        return obj.GetHashCode() ^ obj.GetType().GetHashCode();
    }

    #endregion
}

And this is my Mark class:

  public class Mark
{
    int x;
    int y;

    public Mark(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int X
    {
        get { return x; }
        set { x = value; }
    }

    public int Y
    {
        get { return y; }
        set { y = value; }
    }
}

public class MarkEqualityComparer : IEqualityComparer<Mark>
{
    #region IEqualityComparer<Mark> Members

    public bool Equals(Mark x, Mark y)
    {
        return (x.X == y.X) && (x.Y == y.Y);
    }

    public int GetHashCode(Mark obj)
    {
        return obj.GetHashCode() ^ obj.GetType().GetHashCode();
    }

    #endregion
}

(I can pastebin the code if it’s too much code…)

  • 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-15T08:27:52+00:00Added an answer on May 15, 2026 at 8:27 am

    You can use the EqualityComparer or just override Equals and GetHashCode.

    You must make sure that whatever you consider to be a duplicate is identified as having an equivalent hash code, and returning true when tested for equality.

    My guess is that you weren’t returning equal hash codes. Could you post the code from your equality comparer?

    As a test, you could do:

    var set = new HashSet<MyCustomClass>();
    var a = new MyCustomClass(1,2);
    var b = new MyCustomClass(1,2);
    set.Add(a);
    set.Add(b);
    Assert.IsTrue(a.Equals(b));
    Assert.IsTrue(b.Equals(a));
    Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
    Assert.AreEqual(1, set.Count);
    

    A similar set of tests would be applicable to an equality comparer too.

    EDIT

    Yep, as suspected it’s the hash code function. You need to calculate it based on the values of the type itself. A common enough mistake.

    public int GetHashCode(Mark obj)
    {
        return ((MyCustomClass)obj).point.GetHashCode();
    }
    

    That assumes point is the only state field in your type.

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

Sidebar

Related Questions

Let's say I have this code in Java: HashSet<String> wordSet = new HashSet<String>(); String
Let's say I have a collection of System.Threading.Tasks.Task : HashSet<Task> myTasks = new HashSet<Task>();
I have a HashSet of Identity values that I need to use as the
I have a HashSet<string> DirectoryHolding = new HashSet<string>(); How do I say I want
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>> .
Let's say I have this 2D Array map { 0,0,0,0,7,1,1,1,1,1,1,1,1 }, { 0,7,7,7,7,1,1,1,24,1,1,1,1 },
I have a HashSet<string> that is instantiated using StringComparer.CurrentCultureIgnoreCase and am making extensive use
I need to have a Set (HashSet) such that if I insert a pair
I have a hashset in C# that I'm removing from if a condition is

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.