问这个问题的面试官有点扯淡了,如果用的不多真的记住用法还是不易的,这东西查查微软官方文档就能找到。下面就这个问题回答一下
简述
class Pet{public string Name { get; set; }public int Age { get; set; }}public static void DefaultIfEmptyEx1(){//创建list集合对象List<Pet> pets =new List<Pet>{ new Pet { Name="Barley", Age=8 },new Pet { Name="Boots", Age=4 },new Pet { Name="Whiskers", Age=1 } };//调用DefaultIfEmpty ,如果初始化为空,至少返回一个实例。string[] names =pets.Select(pet => pet.Name).DefaultIfEmpty("wangwu").ToArray();string first = names[0];Console.WriteLine(first);}// 运行结果:wangwu
Enumerable.Empty<T>()
public IEnumerable<Product> ShowProducts(){if (true){return getProduct();//如果方法不为空输出}return Enumerable.Empty<Product>();//返回一个空}