Home > database >  Why don't the following expressions trigger "The result of the expression is always false
Why don't the following expressions trigger "The result of the expression is always false

Time:09-25

I am playing with the following expressions in VS Code and I noticed there are some cases where VS Code shows warning.

if(true==null)
{}
else
{}

The result of the expression is always false since a value of type bool is never equal to null of type bool?.

However VS Code does not complain if I write the following.

if(new Foo()==null)
{}
else
{}

where Foo is a class.

Question

Why don't the above expression produce The result of the expression is always false ...?

CodePudding user response:

First and foremost, static analysis isn't perfect, warnings are there as a best effort to protect you from yourself, there are lots of situations where you may know better.

However, new Foo() can still equal null ... If someone with dirty little fingers decided to override the equality operators :/

Consider the following insidious example

public class Foo
{
   public static bool operator ==(Foo x, Foo y) => true;
   public static bool operator !=(Foo x, Foo y) => false;
}

...

if (new Foo() == null)
   Console.WriteLine("Is null");
else
   Console.WriteLine("Is not null");

Output

Is null

However we can use the is operator to make sure

if (new Foo() is null)
   Console.WriteLine("Is null");
else
   Console.WriteLine("Is not null");

Output

Is not null

Essentially the compiler will convert the above to the following, negating any operator effects

if ((object)new Foo() == null)

As pointed out by @Jeroen Mostert if you are using vanilla Visual Studio or Code this still isn't flagged as always false. Though if you are using resharper it likely will

In short, the compiler really only does basic static checks for these types of things, in certain cases it either cant figure it out, or it was decided not to worry about following complex branches to work out if and expression is actually always true or not.

  •  Tags:  
  • c#
  • Related