草庐IT

IEnumerable

全部标签

c# - 为什么 IEnumerable<T>.ToList<T>() 返回 List<T> 而不是 IList<T>?

扩展方法ToList()返回List.按照相同的模式,ToDictionary()返回Dictionary.我很好奇为什么这些方法不将它们的返回值键入为IList和IDictionary分别。这似乎更奇怪,因为ToLookup将其返回值键入为接口(interface)而不是实际实现。使用dotPeek查看这些扩展方法的源代码或其他反编译器,我们看到以下实现(显示ToList()因为它更短):publicstaticListToList(thisIEnumerablesource){if(source==null)throwError.ArgumentNull("source");ret

c# - 将数组转换为 IEnumerable<T>

假设您有一个基本的Employee这样的类:classEmployee{publicstringName;publicintYears;publicstringDepartment;}然后(在一个单独的类中)我有以下代码片段(我想除了最后一个我都理解):我相信下面的代码片段是有效的,因为数组初始化器创建了一个Employee对象数组,这些对象的类型与分配给的workforce变量相同。Employee[]workforceOne=newEmployee[]{newEmployee(){Name="David",Years=0,Department="software"},newEmpl

c# - 将数组转换为 IEnumerable<T>

假设您有一个基本的Employee这样的类:classEmployee{publicstringName;publicintYears;publicstringDepartment;}然后(在一个单独的类中)我有以下代码片段(我想除了最后一个我都理解):我相信下面的代码片段是有效的,因为数组初始化器创建了一个Employee对象数组,这些对象的类型与分配给的workforce变量相同。Employee[]workforceOne=newEmployee[]{newEmployee(){Name="David",Years=0,Department="software"},newEmpl

c# - 如何对 IEnumerable<string> 进行排序

如何对IEnumerable进行排序按字母顺序排列。这可能吗?编辑:我将如何编写就地解决方案? 最佳答案 与您对任何其他可枚举对象进行排序的方式相同:varresult=myEnumerable.OrderBy(s=>s);或varresult=fromsinmyEnumerableorderbysselects;或(忽略大小写)varresult=myEnumerable.OrderBy(s=>s,StringComparer.CurrentCultureIgnoreCase);请注意,与LINQ一样,这会创建一个新的IEnume

c# - 如何对 IEnumerable<string> 进行排序

如何对IEnumerable进行排序按字母顺序排列。这可能吗?编辑:我将如何编写就地解决方案? 最佳答案 与您对任何其他可枚举对象进行排序的方式相同:varresult=myEnumerable.OrderBy(s=>s);或varresult=fromsinmyEnumerableorderbysselects;或(忽略大小写)varresult=myEnumerable.OrderBy(s=>s,StringComparer.CurrentCultureIgnoreCase);请注意,与LINQ一样,这会创建一个新的IEnume

c# - AsQueryable() 的目的是什么?

AsQueryable()的目的是为了让您可以将IEnumerable传递给可能需要IQueryable的方法,还是有一个将IEnumerable表示为IQueryable的有用理由?例如,它是否应该用于这样的情况:IEnumerableorders=orderRepo.GetAll();//Idon'twanttocreateanothermethodthatworksonIEnumerable,//soIconvertithere.CountOrders(orders.AsQueryable());publicstaticintCountOrders(IQueryableorder

c# - AsQueryable() 的目的是什么?

AsQueryable()的目的是为了让您可以将IEnumerable传递给可能需要IQueryable的方法,还是有一个将IEnumerable表示为IQueryable的有用理由?例如,它是否应该用于这样的情况:IEnumerableorders=orderRepo.GetAll();//Idon'twanttocreateanothermethodthatworksonIEnumerable,//soIconvertithere.CountOrders(orders.AsQueryable());publicstaticintCountOrders(IQueryableorder

c# - 如何在声明它的同一行中初始化 C# 列表。 (IEnumerable 字符串集合示例)

我正在写我的测试代码,我不想写:Listnameslist=newList();nameslist.Add("one");nameslist.Add("two");nameslist.Add("three");我很想写Listnameslist=newList({"one","two","three"});但是{"one","two","three"}不是“IEnumerable字符串集合”。我如何使用IEnumerable字符串集合在一行中初始化它? 最佳答案 varlist=newList{"One","Two","Three"

c# - 如何在声明它的同一行中初始化 C# 列表。 (IEnumerable 字符串集合示例)

我正在写我的测试代码,我不想写:Listnameslist=newList();nameslist.Add("one");nameslist.Add("two");nameslist.Add("three");我很想写Listnameslist=newList({"one","two","three"});但是{"one","two","three"}不是“IEnumerable字符串集合”。我如何使用IEnumerable字符串集合在一行中初始化它? 最佳答案 varlist=newList{"One","Two","Three"

c# - 字典中元素的顺序

我的问题是关于枚举字典元素//DictionarydefinitionprivateDictionary_Dictionary=newDictionary();//addvaluesusingadd_Dictionary.Add("orange","1");_Dictionary.Add("apple","4");_Dictionary.Add("cucumber","6");//addvaluesusing[]_Dictionary["banana"]=7;_Dictionary["pineapple"]=7;//NowletsseehowelementsarereturnedbyI