代码检查:使用 null 检查模式替代对任何非 null 值成功的类型检查
该检查会报告存在显式类型检查,但由于被检查值在非 null 时已知为该类型,因此未增加有用类型信息的模式。 在这种情况下,使用 null 检查模式比重复相同类型检查更清晰。
示例
void M(string s)
{
switch (s)
{
case string _:
break;
}
}
void M(string s)
{
switch (s)
{
case not null:
break;
}
}
快速修复
用 not null 模式替换显式类型检查。
2026年 5月 8日