为什么“dotnet_style_prefer_is_null_check_over_reference_equality_method = true”无法识别“foo == null”?

问题描述 投票:0回答:0

我决定从

foo == null
支票换成
foo is null
。我不必搜索我的所有代码,而是在我的
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
文件中设置
.editorconfig
,运行
dotnet format
,但没有任何反应。我在 Visual Studio 中尝试了
Format Document
,但它也没有用。

查看IDE0041的页面,我看到了这个例子:

// dotnet_style_prefer_is_null_check_over_reference_equality_method = false
if ((object)o == null)
    return;

在我看来,这意味着它应该有效,我四处寻找答案,然后才意识到这项检查需要

(object)
转换。我检查了 Roslyn 代码库,发现果然,UseIsNullCheck 部分只有一个用于相等转换的分析器。

为什么需要这个?在进行空值检查时,我是否应该一直将所有变量都转换为

(object)
,或者这只是实施 IDE0041 示例而不采取下一个逻辑步骤?

c# visual-studio roslyn code-cleanup
© www.soinside.com 2019 - 2024. All rights reserved.