这篇文章主要介绍了c#中Empty()和DefalutIfEmpty()用法,以实例形式分析了针对不同情况下Empty()和DefalutIfEmpty()用法区别,需要的朋友可以参考下
本文实例分析了c#中Empty()和DefalutIfEmpty()用法。分享给大家供大家参考。具体分析如下:
在项目中,当我们想获取IEnumerable
假设有这样一个场景:当商店不营业时,返回一个空的IEnumerable
Product模型。
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
该商店有一个ProductService类,该类根据属bool类型属性IsClosed来决定是否返回空的IEnumerable private static IEnumerable public IEnumerable 以上就是c#中Empty()和DefalutIfEmpty()用法分析的详细内容,更多请关注0133技术站其它相关文章!
{
public bool IsClosed { get; set; }
{
return new List
{
new Product(){Id = 1, Name = "Product1", Price = 85M},
new Product(){Id = 2, Name = "Product2", Price = 90M}
};
}
{
if (!IsClosed)
{
return GetAllProducts();
}
return new List
}
}