c# 判断是否为空 order by 报错

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;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值