
null ,以及正确抛出 ArgumentNullException。以前可以使用样板代码的变体来验证方法参数是否为空:.
public static void M(string s)
{
if (s is null)
{
throw new ArgumentNullException(nameof(s));
}
// Body of the method
}
使用新的参数 null 检查,可以通过添加 !! 到参数名,自动执行空值检查:
public static void M(string s!!)
{
// Body of the method
}
自动生成的空值检查代码将在方法主体的代码之前执行。对于构造器,该空值检查发生在字段初始化、调用 base 构造函数和调用 this 构造函数之前。