c# - Why don't the following expressions trigger "The result of the expression is always false ..."?
问题描述
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 typebool
is never equal tonull
of typebool?
.
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 ...
?
解决方案
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 can't 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.
推荐阅读
- c# - 在 powershell 脚本中使用 c# 函数的正确语法是什么?
- r - 如何通过 2+ 个条件自定义因子变量的订单级别?
- sql - 向表格查询添加日期条件
- sql-server - 创建表,列不能为空,其他列不能为空,以及如何解决
- angular - 错误:位置 0 处的 JSON 中的意外标记 u
- c# - WCF自托管:身份验证方案匿名的http请求被禁止
- rest - 使用 Kubernetes 编排的 RESTful 服务
- php - 无法在 Jasmin SMS 网关中发送 unicode 内容
- solr - Nutch 在索引 SOLR 时出现 Shuffle 错误。
- ruby-on-rails - Rails + S3:解析存储桶并在控制器外部加载图像