>
>
>
V3197. The compared value inside the 'O…


V3197. The compared value inside the 'Object.Equals' override is converted to a different type that does not contain the override.

The analyzer detected a potential error related to checking an incorrect type in the overridden 'Equals' method.

Look at the example:

private class FirstClass
{
  ....
  public override bool Equals(object obj)
  {
    SecondClass other = obj as SecondClass;   // <=

    if (other == null)
    {
      return false;
    }
    return Equals(other);
  }
  public bool Equals(FirstClass other)
  {
    ....
  }
}

In the overridden 'Equals' method of the 'FirstClass' class, an error occurs when checking the 'obj' type. The 'FirstClass' type should be used instead of the 'SecondClass' type.

As a result, if an object of the 'FirstClass' type is passed to the overridden 'Equals' method, the method always returns 'false'.

Moreover, if an object of the 'SecondClass' type is passed as a parameter, the same overridden 'Equals' method is called. It results in recursion and a 'StackOverflowException'.

Let's look at the fixed version:

private class FirstClass
{
  ....
  public override bool Equals(object obj)
  {
    FirstClass other = obj as FirstClass;  

    if (other == null)
    {
      return false;
    }
    return Equals(other);
  }
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3197 diagnostic.