SonarQube warning: Classes implementing "IEquatable<T>" should be sealed
Hi,
SonarQube is giving out the warning with the following explanation:
When a class implements the IEquatable<T> interface, it enters a contract that states, "I know how to compare two instances of type T or any type derived from T for equality.". However, if that class is derived, it is improbable that the base class will know how to make a meaningful comparison. Therefore that implicit contract is now broken.
Alternatively, IEqualityComparer<T> provides a safer interface and is used by collections or Equals could be made virtual.
This rule raises an issue when an unsealed, public, or protected class implements IEquitable<T> and the Equals are neither virtual nor abstract
I am looking forward to hearing your thoughts on it.
Thanks