C#

C#

Made by DeepSource

Comparing an instantiated object with null will always evaluate to false CS-R1125

Anti-pattern
Major

Comparing an object that is instantiated using the new keyword against null is useless. If this object is being compared against null using the == operator, the expression will always evaluate to false and as a result, statements under this if statement will never be executed. However, comparing against null using the != operator will always evaluate to true and any statements under this if statement will always be executed. It is therefore recommended that you refactor your code accordingly.

Bad Practice

var c = new C();

// Always true. Statements under this `if` statement
// will always be executed.
if (c != null)
{
    // Do something
}

// Always false. Statements under this `if` statement
// will never be executed.
if (c == null)
{
    // Do something else
}

Recommended

var c = new C();
// Do something