代码检查:条件访问限定符表达式已知为 null 或非 null。
当限定符在该程序点已知时,此检查会报告条件访问表达式。 实际上,分析器已证明限定符肯定为非 null 或肯定为 null,因此 ?. 不再表示真实条件。
这种情况最常发生在控制流已经确定值为非 null 之后,或在非可空值类型结果上使用 ?. 时。
当限定符确定为 null 时,整个条件访问实际上就是无效代码,通常应重写或移除。
if (text != null)
{
int length = text?.Length ?? 0;
}
if (text != null)
{
int length = text.Length;
}
2026年 5月 8日