判断是否为null 应该使用 is null
避免使用 “== null”
因为 后者可能调用 “operator ==”
另外1:C# 9.0 引入了 is not
另外2:有用的判断为null 后抛出异常的简洁语句:
public static int CountNumberOfSInName(string name)
{
_ = name ?? throw new ArgumentNullException(nameof(name));
return name.Count(c => char.ToLower(c).Equals('s'));
}
Reference:
https://stackoverflow.com/questions/6417902/checking-if-an-object-is-null-in-c-sharp
https://www.thomasclaudiushuber.com/2020/03/12/c-different-ways-to-check-for-null/