ReSharper 2026.3 Help

代码检查:整理空值检查模式

在使用 类型测试“is”运算符检查表达式是否为空时,您可以选择两种空值检查模式:

  • 使用 “not null”模式,使表达式更具可读性。

  • 使用对象模式语法“{ }”,使表达式更灵活,允许您在其后声明局部变量。

public static void Test(object? obj) { if (obj is not null) Console.WriteLine("not null"); }
public static void Test(object? obj) { if (obj is { }) Console.WriteLine("not null"); }

By default, ReSharper highlights object pattern syntax { } in null-checking expressions and suggests replacing them with the not null pattern:

ReSharper 语法样式检查:使用“not null”模式

如果您更喜欢使用 { } 模式,您可以 更改相应的首选项 ,ReSharper 将帮助您相应地替换 not null 模式:

ReSharper 语法样式检查:使用“{ }”模式

更多信息,请参阅 代码语法样式:空值检查模式。

2026年 11月 6日