扩展方法where方法查询不到数据,不会抛异常,也不是返回的null

如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下:

Product类:

 public class Product
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double price;

        public double Price
        {
            get { return price; }
            set { price = value; }
        }

        public override string ToString()
        {
            return string.Format("{0}:{1}", Name, Price);
        }
    }
View Code

 

Main函数:

 static void Main(string[] args)
        {
            Console.WriteLine("验证where方法查询不到数据,不会抛异常,也不是返回的null。");
            Console.WriteLine();

            List<Product> list = new List<Product> 
            {
                new Product{Name="三文鱼",Price=205.5},
                new Product{Name="鲫鱼",Price=15.5},
                new Product{Name="秋刀鱼",Price=10},
                new Product{Name="猪肉",Price=18.5},
                new Product{Name="牛肉",Price=70.5},
                new Product{Name="驴肉",Price=100}
            };
            Console.WriteLine("------------FindAll方法(单价大于30的商品)-----------");
            list.FindAll(p => p.Price > 30).ForEach(Console.WriteLine);
            Console.WriteLine();

            Console.WriteLine("------------Where方法(单价大于30的商品)-----------");
            foreach (var item in list.Where(p=>p.Price>30))
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            //验证where方法查询不到数据,不会抛异常,也不是返回的null
            Console.WriteLine("------------Where方法(单价大于30000的商品)---------");
            var num=list.Where(p => p.Price > 30000).Count();
            Console.WriteLine("有{0}个单价大于30000的商品。",num);
            Console.ReadKey();
        }
View Code

 

 

假如 list.Where(p => p.Price > 30000)  返回null,则list.Where(p => p.Price > 30000).Count()会抛异常。

事实上代码正确地运行了,即验证了:

扩展方法where方法查询不到数据,不会抛异常,也不是返回的null

运行截图如下:

转载于:https://www.cnblogs.com/527289276qq/p/4444126.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值