rspec/rules/S3897/csharp/rule.adoc

37 lines
757 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
The ``++IEquatable<T>++`` interface has only one method in it: ``++Equals(<T>)++``. If you've already written ``++Equals(T)++``, there's no reason not to explicitly implement ``++IEquatable<T>++``. Doing so expands the utility of your class by allowing it to be used where an ``++IEquatable++`` is called for.
**Note**: Classes that implement ``++IEquatable<T>++`` should also be ``++sealed++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
class MyClass // Noncompliant
{
public bool Equals(MyClass other)
{
//...
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
sealed class MyClass : IEquatable<MyClass>
{
public override bool Equals(object other)
{
return Equals(other as MyClass);
}
public bool Equals(MyClass other)
{
//...
}
}
----