Suppose I have two classes that implement the same interface, as in the example in C#
below:
public interface ICommonInterface
{
string Text { get; }
int Count { get; }
}
public class ImplementationA: ICommonInterface
{
// Implementation A
}
public class ImplementationB: ICommonInterface
{
// Implementation B
}
I want to define equality for implementations of ITargetOccurence
as ITargetOccurence.Text
and ITargetOccurence.Count
being both equal.
What is a good way of defining such equality, and why?
Possible Solutions I Have Considered:
1) Implementing a Static Method Helper For Equality
I could make equality of interfaces something that can be accessed via a static helper class, such as:
public static class CommonInterfaceHelper
{
public static bool AreEqual(ICommonInterface A, ICommonInterface B)
{
// ...
}
}
This does not make use of any equality pattern in the language, though.
2) Overriding Equality Operator In The Classes
I could do something similar to the implementation suggested in this question: https://stackoverflow.com/questions/22101703/overriding-equals-in-c-sharp-interface-implementation
public class ImplementationA: ICommonInterface
{
public override bool Equals(object obj)
{
ICommonInterface other = obj as ICommonInterface;
// ...
}
}
// Same thing for ImplementationB
I believe that equality returning true
for instances of another class is counter intuitive, though.
3) Changing ICommonInterface
to a struct
Instead of making ICommonInterface
an interface, it could perhaps be a struct
that has a better defined equality. The resulting implementation could be something like this:
public struct CommonEnum
{
string Text;
int Count;
}
public class ImplementationA
{
// Implementation A
public static implicit operator CommonEnum(ImplementationA imp)
{
CommonEnum commonEnum = new CommonEnum();
commonEnum.Text = imp.Text;
commonEnum.Count = imp.Count;
};
}
// Same thing for ImplementationB
This unties the ImplementationA
and ImplementationB
, though.
4) Argue that interfaces (ICommonInterface
) should not have a equality constraint
Interfaces are just specifications on what methods a class should have. In that case, one could argue that ICommonInterface
should not have a "equality constraint" and that one should check each method individually if comparing equality.
5) Make Equality a Method Defined by the Interface
With the same reasoning as (4) one could argue that if equality was to be expected, the Interface should define:
public interface ICommonInterface
{
string Text { get; }
int Count { get; }
bool IsEqual(ICommonInterface other);
}
This also does not make use of the equality patterns the language provides, though.
Is there any standard way of defining this equality, by following a given set of good practices or principles?
For clarification regarding acceptance criteria for answers, I believe that an answer to this question should be either:
- An argument that this is an "opinion based" subject and that there are no set of good practices or principles that indicate one of the given approaches is preferred.
- An argument that one of the approaches is preferred by following a given set of good practices or principles, with some links or explanation of what principles were used.
IEquatable<T>
for your concrete implementations (i.e. your classes). See docs.microsoft.com/en-us/dotnet/api/…