C#

C#

Made by DeepSource

Operator implementation refers to itself and may cause recursive calls CS-W1079

Bug risk
Critical

C# allows you to define operators just like normal methods. However, these operators should not refer to themselves in their implementation. Doing so may result in unterminated recursive calls.

Bad Practice

public static bool operator ==(Complex c1, Complex c2)
{
    // We're implementing `==` for `Complex` class. However, the following binary
    // expression uses `==` for comparing an instance of `Complex` class. This causes a recursive
    // call.
    if (c1 == ...)
    {
        // ...
    }
}

Recommended

public static bool operator ==(Complex c1, Complex c2)
{
    // ...
    // No more reference to operators such as `==` or `!=`.
    if (c1.real == c2.real && ...)
    {
        // ...
    }
}