c# 判断 是否为空的时候, 使用 is null 这个写法
if (other is null)
{
throw new ArgumentNullException();
}
刚才 在使用linq 的 order by , thenby 排序算法 的时候, 因为实体重写了 == 运算符, , 在重写IComparable 的 接口
的CompareTo函数, 里面判断空 使用的 == 之久就疯狂报错 空引用, 这里似乎出现了循环引用, 然后就分开报错 空引用, 之后才发现 是这里出错了
public int CompareTo(YearAndMonth other)
{
// if(other == null) //== 严重错误
if (other is null)
{
throw new ArgumentNullException();
}
if (Year == other.Year && Month == other.Month)
{
return 0;
}
if (Year > other.Year)
{
return 1;
}
if (Year == other.Year && Month > other.Month)
{
return 1;
}
return -1;
}
public static bool operator ==(YearAndMonth b, YearAndMonth c)
{
return b.CompareTo(c) == 0;
}