Hash set comapre with custom object

Dani_S 4,501 Reputation points
2024-02-18T08:49:49.0433333+00:00

Hi, I i have custom hash set. Is it the way to use Contains by implementing ScanHistoryIdentifierCompare ? HashSet<ScanHistoryIdentifier> hashSetScanHistoryIdentifier = new HashSet<ScanHistoryIdentifier>(new ScanHistoryIdentifierCompare()); var scanHistoryIdentifier = new ScanHistoryIdentifier(route.Value.Portal, route.Value.UserBy); if (!hashSetScanHistoryIdentifier.Contains(scanHistoryIdentifier)) { // do my logic } public class ScanHistoryIdentifier { public ScanHistoryIdentifier(string portal, string user) { Portal = portal; User = user; } public string Portal{ get; set; } public string User { get; set; }

  public override bool Equals(object obj)
  {
      return obj is ScanHistoryIdentifier scanHistoryIdentifier &&
          Portal.ToLower() == scanHistoryIdentifier.Portal.ToLower() && 
          User.ToLower() == scanHistoryIdentifier.User.ToLower();
  }
  public override int GetHashCode()
  {
      return HashCode.Combine(Portal, User);
  }

} public class ScanHistoryIdentifierCompare : IEqualityComparer<ScanHistoryIdentifier> { public bool Equals(ScanHistoryIdentifier x, ScanHistoryIdentifier y) { if (x.Portal.ToLower() == y.Portal.ToLower() && x.User.ToLower() == y.User.ToLower()) { return true; } else { return false; } } public int GetHashCode(ScanHistoryIdentifier obj) { return obj.GetHashCode(); } }

Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2024-02-20T07:51:49.57+00:00

    Hi,.@דני שטרית . Welcome to Microsoft Q&A .

    In .NET, when you use a collection such as HashSet<T>, Contains() method is used to check whether the collection contains a specific element. The behavior of Contains() relies on the implementation of two important methods: Equals() and GetHashCode().

    Here's how it works:

    Equals():

    This method is used to determine if two objects are equal. When you call Contains() on a collection, it internally calls the Equals() method of each element in the collection to check for equality.

    GetHashCode():

    This method returns a hash code for the object. Hash codes are used to quickly identify objects within a collection. When you insert or look up elements in a collection like HashSet<T>, the hash code is used to quickly find the bucket in which the object should be stored or searched.

    Now, here's how it relates to your scenario:

    Hash Set with Case-Insensitive Comparison:

    By overriding GetHashCode() to return a hash code based on the lowercase values of Portal and User, and using a case-insensitive comparison in the Equals() method, you ensure that objects with equal lowercase values for Portal and User will have the same hash code. This means that if you have two objects with the same lowercase Portal and User, they will end up in the same bucket in the hash set.

    Contains() Method:

    When you call Contains() on the hash set, it first calculates the hash code of the element you're checking for. Then, it looks in the bucket corresponding to that hash code to see if any element there is equal to the one you're checking for, using the Equals() method. If it finds a match, Contains() returns true; otherwise, it returns false.

    By ensuring that Equals() and GetHashCode() are consistent with your equality comparison logic (in this case, case-insensitive comparison of Portal and User), you could use Contains() effectively to check for the presence of elements in your hash set.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2024-02-18T10:16:37.8266667+00:00

    I think that equal objects should return the same hash code, therefore the function can be rewritten:

    public override int GetHashCode( )
    {
       return HashCode.Combine( Portal.ToLower(), User.ToLower() );
    }
    

    In this case the ScanHistoryIdentifierCompare is not needed. The hashset will store cased-insesitive data and Contains will work.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.