0

Hi I am trying to add in IEquatable to my program and I have no clue if I need to add a unique id basicly and a hashcode? They are using the shd number as a unique id in the IEquatable but they give the value to it in the constructor and I asked on this site if the constructor was needed to look like it does in the documentation and I got a no. So now I am confused can someone give me an easier example of IEquatable then the documentation does? here is the link for the documentation https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1?view=netcore-3.1 I am just trying to get contains for a custom object list to work.

My code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataConverter.Objects
{
    public class Category : IEquatable<Category>
    {
       public string _Name { get; set; }
       public string _Id { get; set;}

       private string _HomeCategory { get; set; }  
       public string _prestaId { get; set; }

       public bool _inUse { get; set; }






        public Category(string Name, string Id, string HomeCategory, bool inUse)
        {
            _Name = Name;
            _Id = Id;
            _HomeCategory = HomeCategory;
            _inUse = inUse;

        }

        public bool Equals(Category other)
        {
            if (other == null)
                return false;

            if()
        }
    }
}
  • 2
    What is a "shd number"? (Also some punctuation could help - at least me - to understand your question better) – Christian.K May 04 '20 at 09:59
  • 1
    can you share your code, maybe we will help to understand the question? – Mohammed Sajid May 04 '20 at 10:00
  • `I am just trying to get contains for a custom object list to work` - then implement `GetHashCode` and `Equals`? – GSerg May 04 '20 at 10:02
  • done code added –  May 04 '20 at 10:03
  • yes but the question is how to implement this i mean if there is a simpler way to give an example of IEquatable –  May 04 '20 at 10:04
  • @JensSvensson chek this documentation https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netcore-3.1 – Mohammed Sajid May 04 '20 at 10:11
  • `but the question is how to implement this` - only you know when two of your objects should be considered equal. So you put that comparison in `Equals`. And then you change `GetHashCode` so that it would return the same hash for objects that would be considered equal by `Equals`. – GSerg May 04 '20 at 10:16

1 Answers1

1

IEquatable<T> is just an interface that tells you that the current type has value equality semantics, not reference equality. What the logic is behind what makes two distinct objets equal or not is completely dependant on the business logic of your application, not how you correctly or incorrectly implement the interface.

What makes two categories be the same in your domain? The same Id? Then your equals method would be:

public bool Equals(Category other)
{
    if (other == null)
        return false;

    return _Id == other._Id;
}

The same Id and name?

public bool Equals(Category other)
{
    if (other == null)
        return false;

    return _Id == other._Id &&
           _Name == other._Name;
}

And so on. Its you who decides what makes two categories equal each other.

Now, about the hash code. The basic rule a hash code must comply with is: Two categories that are equal must have the same hash code. Do note that this does not mean that two categories with the same hash code are equal, hash codes can collide, there is no problem with that at all. Also, hash codes should not change if the object changes, so ideally they should be built upon data that can not be changed.

Ok, so how would you implement your hash code in your particular scenario?

Case 1:

public override int GetHashCode() => _Id.GetHashCode();

Case 2:

public override int GetHashCode() => _Id.GetHashCode() ^
                                     _Name.GetHashCode();

Both hashes will be consistent with their Equals counterpart. The only problem is that they are built on potentially mutable data (both Id and Name have setters) so if any of those change, your haschcode would change too, and that is usually a bad idea.

Depending on your scenario you might have to deal with this problem differently. Is your data likely to mutate while performing any hash sensitive operations? Linq for example used hashes quite extensively...

InBetween
  • 32,319
  • 3
  • 50
  • 90