【问题标题】:Can my custom method satisfy Pragma Warnings?我的自定义方法可以满足 Pragma 警告吗?
【发布时间】:2022-07-06 05:00:37
【问题描述】:

例如,当我执行string.IsNullOrWhiteSpace("") 时,这满足了编译指示警告:

CS8604:可能的空引用参数

现在,如果我定义了一个名为 "".IsNull() 的扩展方法,是否有可能让 IDE/编译器将其识别为 CS8604 的有效处理程序?

public static bool IsNull(this string? s) => string.IsNullOrWhiteSpace(s);

【问题讨论】:

    标签: c# .net-6.0 visual-studio-2022 pragma


    【解决方案1】:

    是的 - 你想要 attributes used by null-state static analysis 之一。

    在这种特殊情况下,我想你想要NotNullWhen

    public static bool IsNull([NotNullWhen(false)] this string? s) => 
        string.IsNullOrWhiteSpace(s);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多