2023-05-03 11:06:20 +02:00
== Why is this an issue?
2022-02-28 15:20:24 +01:00
When a class implements the `IEquatable<T>` interface, it enters a contract that, in effect, 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 very unlikely that the base class will know how to make a meaningful comparison. Therefore that implicit contract is now broken.
2021-04-28 16:49:39 +02:00
2022-02-28 15:20:24 +01:00
Alternatively `IEqualityComparer<T>` provides a safer interface and is used by collections or `Equals` could be made `virtual`.
2021-04-28 16:49:39 +02:00
2022-03-04 14:49:40 +01:00
This rule raises an issue when an unsealed, `public` or `protected` class implements `IEquitable<T>` and the `Equals` is neither `virtual` nor `abstract`.
2021-04-28 16:49:39 +02:00
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
using System;
namespace MyLibrary
{
2022-02-28 15:20:24 +01:00
public class Base : IEquatable<Base> // Noncompliant
2021-04-28 16:49:39 +02:00
{
2022-02-28 15:20:24 +01:00
public bool Equals(Base other)
2021-04-28 16:49:39 +02:00
{
2022-02-28 15:20:24 +01:00
if (other == null) { return false; }
2021-04-28 16:49:39 +02:00
// do comparison of base properties
2022-02-28 15:20:24 +01:00
return true;
2021-04-28 16:49:39 +02:00
}
2022-02-28 15:20:24 +01:00
public override bool Equals(object other) => Equals(other as Base);
2021-04-28 16:49:39 +02:00
}
class A : Base
{
2022-02-28 15:20:24 +01:00
public bool Equals(A other)
2021-04-28 16:49:39 +02:00
{
2022-02-28 15:20:24 +01:00
if (other == null) { return false; }
2021-04-28 16:49:39 +02:00
// do comparison of A properties
return base.Equals(other);
}
2022-02-28 15:20:24 +01:00
public override bool Equals(object other) => Equals(other as A);
2021-04-28 16:49:39 +02:00
}
class B : Base
{
2022-02-28 15:20:24 +01:00
public bool Equals(B other)
2021-04-28 16:49:39 +02:00
{
2022-02-28 15:20:24 +01:00
if (other == null) { return false; }
2021-04-28 16:49:39 +02:00
// do comparison of B properties
2022-02-28 15:20:24 +01:00
return base.Equals(other);
2021-04-28 16:49:39 +02:00
}
2022-02-28 15:20:24 +01:00
public override bool Equals(object other) => Equals(other as B);
2021-04-28 16:49:39 +02:00
}
2022-02-28 15:20:24 +01:00
internal class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
Console.WriteLine(a.Equals(b)); // This calls the WRONG equals. This causes Base.Equals(Base)
// to be called which only compares the properties in Base and ignores the fact that
// a and b are different types. In the working example A.Equals(Object) would have been
// called and Equals would return false because it correctly recognizes that a and b are
// different types. If a and b have the same base properties they will be returned as equal.
}
2021-04-28 16:49:39 +02:00
}
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
using System;
namespace MyLibrary
{
public sealed class Foo : IEquatable<Foo>
{
public bool Equals(Foo other)
{
// Your code here
}
}
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
https://msdn.microsoft.com/en-us/library/ms132151(v=vs.110).aspx[IEqualityComparer<T> Interface]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Seal class "XXX" or implement "IEqualityComparer<T>" instead.
=== Highlighting
Class declaration
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 21 Jun 2017, 11:26:00 Amaury Levé wrote:
We decided not to recommend sealing the class when implementing ``++Equals(object)++`` as it is possible for sub-classes to change the behavior. Besides, we also added an exception to the rule to say we don't report if the ``++Equals(T)++`` is ``++virtual++``.
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]