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. |
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders:
Take
a chance!