草庐IT

ienumerator

全部标签

c# - 我可以有一个方法返回 IEnumerator<T> 并在 foreach 循环中使用它吗?

我需要设置表单上每个文本框的高度,其中一些文本框嵌套在其他控件中。我以为我可以做这样的事情:privatestaticIEnumeratorFindTextBoxes(ControlrootControl){foreach(ControlcontrolinrootControl.Controls){if(control.Controls.Count>0){//RecursivelysearchforanyTextBoxeswithineachchildcontrolforeach(TextBoxtextBoxinFindTextBoxes(control)){yieldreturnte

c# - 我可以有一个方法返回 IEnumerator<T> 并在 foreach 循环中使用它吗?

我需要设置表单上每个文本框的高度,其中一些文本框嵌套在其他控件中。我以为我可以做这样的事情:privatestaticIEnumeratorFindTextBoxes(ControlrootControl){foreach(ControlcontrolinrootControl.Controls){if(control.Controls.Count>0){//RecursivelysearchforanyTextBoxeswithineachchildcontrolforeach(TextBoxtextBoxinFindTextBoxes(control)){yieldreturnte

c# - 将数组传递给采用参数 object[] 或 IEnumerable<T> 的函数

我想将一个自定义对象数组传递给类似String.Join的函数具有以下签名:publicstaticstringJoin(stringseparator,paramsObject[]values)publicstaticstringJoin(stringseparator,IEnumerablevalues)如果我这样调用函数:vararr=newMyClass[]{newMyClass(),newMyClass()};stringtext=string.Join("\n",arr);我得到一个编译器错误:Thecallisambiguousbetweenthefollowingmet

c# - 将数组传递给采用参数 object[] 或 IEnumerable<T> 的函数

我想将一个自定义对象数组传递给类似String.Join的函数具有以下签名:publicstaticstringJoin(stringseparator,paramsObject[]values)publicstaticstringJoin(stringseparator,IEnumerablevalues)如果我这样调用函数:vararr=newMyClass[]{newMyClass(),newMyClass()};stringtext=string.Join("\n",arr);我得到一个编译器错误:Thecallisambiguousbetweenthefollowingmet

c# - 为什么将 IDictionary<TKey, TValue> 向上转换为 IEnumerable<object> 会失败?

请看下面的代码片段:(IEnumerable)newDictionary()上面的转换会抛出一个无效的转换异常。实际上,IDictionary还间接实现了IEnumerable,因为它还实现了ICollection.也就是说,整个Actor阵容应该是有效的。事实上,对我来说更奇怪的是,如果我在调试器watch插槽上运行整个转换,它会工作!这是怎么回事? 最佳答案 那个字典确实实现了IEnumerable>和IEnumerable,但是IEnumerable结构与IEnumerable不同一个对象。Varianceonlyworksf

c# - 为什么将 IDictionary<TKey, TValue> 向上转换为 IEnumerable<object> 会失败?

请看下面的代码片段:(IEnumerable)newDictionary()上面的转换会抛出一个无效的转换异常。实际上,IDictionary还间接实现了IEnumerable,因为它还实现了ICollection.也就是说,整个Actor阵容应该是有效的。事实上,对我来说更奇怪的是,如果我在调试器watch插槽上运行整个转换,它会工作!这是怎么回事? 最佳答案 那个字典确实实现了IEnumerable>和IEnumerable,但是IEnumerable结构与IEnumerable不同一个对象。Varianceonlyworksf

c# - 通过等待每个任务异步转换 IEnumerable<Task<T>>

今天我想知道如何通过等待每个任务来转换任务列表。考虑以下示例:privatestaticvoidMain(string[]args){try{Run(args);Console.ReadLine();}catch(Exceptionex){Console.WriteLine(ex.ToString());Console.ReadLine();}}staticasyncTaskRun(string[]args){//Version1:doescompile,butuglyandListoverheadvartasks1=GetTasks();ListgainStrings1=newLis

c# - 通过等待每个任务异步转换 IEnumerable<Task<T>>

今天我想知道如何通过等待每个任务来转换任务列表。考虑以下示例:privatestaticvoidMain(string[]args){try{Run(args);Console.ReadLine();}catch(Exceptionex){Console.WriteLine(ex.ToString());Console.ReadLine();}}staticasyncTaskRun(string[]args){//Version1:doescompile,butuglyandListoverheadvartasks1=GetTasks();ListgainStrings1=newLis

c# - 如何将 ienumerable 集合附加到 C# 中的现有列表

我有三个函数,它们返回一个IEnumerable集合。现在我想将所有这些组合成一个列表。那么,有什么方法可以将IEnumerable中的项目附加到列表中。我的意思是没有每个循环? 最佳答案 好吧,有些东西必须循环...但是在LINQ中,您可以轻松地使用Concat和ToList扩展方法:varbigList=list1.Concat(list2).Concat(list3).ToList();请注意,这将创建一个新列表,而不是将项目附加到现有列表。如果您想将它们添加到现有列表中,List.AddRange可能是您想要的:bigLis

c# - 如何将 ienumerable 集合附加到 C# 中的现有列表

我有三个函数,它们返回一个IEnumerable集合。现在我想将所有这些组合成一个列表。那么,有什么方法可以将IEnumerable中的项目附加到列表中。我的意思是没有每个循环? 最佳答案 好吧,有些东西必须循环...但是在LINQ中,您可以轻松地使用Concat和ToList扩展方法:varbigList=list1.Concat(list2).Concat(list3).ToList();请注意,这将创建一个新列表,而不是将项目附加到现有列表。如果您想将它们添加到现有列表中,List.AddRange可能是您想要的:bigLis