0

Let's say I have an this code:

public interface IInterface
{
    //  Properties...
}

internal class Realisation : IInterface, IEquatable<IInterface>
{
    //  Properties...

    public bool Equals(IInterface other)
    {
        //  ...
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as IInterface);
    }

    public override int GetHashCode()
    {
        //  ...
    }
}

The purpose is to not have duplication/different version of the equality code.

I don't have access to Realisation because the code is in a library. If I have to call the equality comparer of IInterface, does it make sense to call EqualityComparer<IInterface>.Default here? Will it use the Equals(IInterface other) implementation? Or should the equality implementation lies outside of the class? Should I provide a custom IEqualityComparer<IInterface>?

Edit

  • Added Equals(object obj) and GetHashCode() overrides
gfache
  • 606
  • 6
  • 15

1 Answers1

0

I suggest it better you put your comparer outside you class , as it satisfy Single responsibility principle - class do only one task raher than multiple thing(it should not become super clas) and (Open Close Principle)Extend your class rather than modifying your class, which means keep code of equal in seprate class rahter then in your class. (this is my suggestion )


One more thing if you have access to class code rather than making use of IEquatable , Override base object class method like Equals & GetHashcode.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Yeah I did not mentioned the `Equals` & co override. Added – gfache Dec 19 '17 at 11:21
  • @gfache - it just suggestion but if you are going for IEquitable interface implementation do create new class implement it there and do equalizing operation – Pranay Rana Dec 19 '17 at 11:22