All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S3897.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

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.

Noncompliant code example

class MyClass  // Noncompliant
{
  public bool Equals(MyClass other)
  {
    //...
  }
}

Compliant solution

sealed class MyClass : IEquatable<MyClass>
{
  public override bool Equals(object other)
  {
    return Equals(other as MyClass);
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy