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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:22+00:00 2026-05-16T17:07:22+00:00

I am expecting a HashSet that has been created with a specified EqualityComparer to

  • 0

I am expecting a HashSet that has been created with a specified EqualityComparer to use that comparer on a Remove operation. Especially since the Contains operations returns true!

Here is the code I am using:

public virtual IEnumerable<Allocation> Allocations { get { return _allocations; } }
private ICollection<Allocation> _allocations; 

public Activity(IActivitySubject subject) {    // constructor
    ....
    _allocations = new HashSet<Allocation>(new DurationExcludedEqualityComparer());
}

public virtual void ClockIn(Allocation a)
{
    ...
    if (_allocations.Contains(a)) 
        _allocations.Remove(a);
    _allocations.Add(a);
}

Below is some quick and dirty LINQ that gets me the logic I want, but I am guessing the HashSet remove based on the EqualityComparer would be significantly faster.

public virtual void ClockIn(Allocation a)
{
    ...
    var found = _allocations.Where(x => x.StartTime.Equals(a.StartTime) && x.Resource.Equals(a.Resource)).FirstOrDefault();
    if (found != null)
    {
            if (!Equals(found.Duration, a.Duration))
            {
                found.UpdateDurationTo(a.Duration);
            }
    }
    else
    {
            _allocations.Add(a);
    }

Can anyone suggest why the Remove would fail when the Contains succeeds?

Cheers,
Berryl

=== EDIT === the comparer

public class DurationExcludedEqualityComparer : EqualityComparer<Allocation>
{
    public override bool Equals(Allocation lhs, Allocation rhs)
    {
        if (ReferenceEquals(null, rhs)) return false;
        if (ReferenceEquals(lhs, null)) return false;
        if (ReferenceEquals(lhs, rhs)) return true;

        return 
            lhs.StartTime.Equals(rhs.StartTime) &&
            lhs.Resource.Equals(rhs.Resource) && 
            lhs.Activity.Equals(rhs.Activity);
    }

    public override int GetHashCode(Allocation obj) {
        if (ReferenceEquals(obj, null)) return 0;
        unchecked
        {
            var result = 17;
            result = (result * 397) ^ obj.StartTime.GetHashCode();
            result = (result * 397) ^ (obj.Resource != null ? obj.Resource.GetHashCode() : 0);
            result = (result * 397) ^ (obj.Activity != null ? obj.Activity.GetHashCode() : 0);
            return result;
        }
    }
}

=== UPDATE – FIXED ===

Well, the good news is that HashSet is not broken and works exactly as it should. The bad news, for me, is how incredibly stupid I can be when not being able to see the forest while examining the leaves on the trees!

The answer is actually in the posted code above, if you look at the class creating & owning the HashSet, and then taking another look at the Comparer to find out what is wrong with it. Easy points for the first person to spot it.

Thanks to all who looked at the 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-16T17:07:23+00:00Added an answer on May 16, 2026 at 5:07 pm

    Well, your code that “works” appears to look at StartTime and Resource while ignoring Activity, whereas your IEqualityComparer<Allocation> implementation looks at all three. Could your problem be related to that?

    Also: are your StartTime, Resource, and Activity properties unchanging? Otherwise, since they affect your GetHashCode result, I think you run the risk of breaking your HashSet<Allocation>.

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

Sidebar

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.