本文介绍了处理可能对 IEnumerable 进行多次枚举的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的代码中,我需要多次使用 IEnumerable,导致 ReSharper 错误可能多次枚举 IEnumerable".>示例代码:public ListFoo(IEnumerable 对象){if (objects == null || !objects.Any())抛出新的 ArgumentException();var firstObject = objects.First();var list = DoSomeThing(firstObject);var secondList = DoSomeThingElse(objects);list.AddRange(secondList);退货清单;}我可以将 objects 参数更改为 List,然后避免可能的多重枚举,但是我没有得到我可以处理的最高对象.我可以做的另一件事是在方法的开头将 IEnumerable 转换为 List: public ListFoo(IEnumerable 对象){var objectList = objects.ToList();//...}但这只是尴尬.在这种情况下你会怎么做? 解决方案 将 IEnumerable 作为参数的问题在于它告诉调用者我希望枚举它".它不会告诉他们您希望枚举多少次.我可以将对象参数更改为 List,然后避免可能的多重枚举,但是我没有得到我可以处理的最高对象.取最高目标的目标是崇高的,但它为太多假设留下了空间.你真的希望有人将 LINQ to SQL 查询传递给这个方法,只让你枚举它两次(每次都得到可能不同的结果?)这里缺少的语义是调用者可能不花时间阅读方法的细节,可能会假设您只迭代一次 - 所以他们传递给您一个昂贵的对象.您的方法签名不表明任何一种方式.通过将方法签名更改为 IList/ICollection,您至少可以让调用者更清楚您的期望是什么,并且他们可以避免代价高昂的错误.否则,大多数查看该方法的开发人员可能会假设您只迭代一次.如果使用 IEnumerable 如此重要,您应该考虑在方法的开头执行 .ToList().很遗憾.NET 没有 IEnumerable + Count + Indexer 的接口,没有添加/删除等方法,我怀疑这可以解决这个问题.In my code I need to use an IEnumerable<> several times, resulting in the ReSharper error of "Possible multiple enumeration of IEnumerable".Sample code:public List<object> Foo(IEnumerable<object> objects){ if (objects == null || !objects.Any()) throw new ArgumentException(); var firstObject = objects.First(); var list = DoSomeThing(firstObject); var secondList = DoSomeThingElse(objects); list.AddRange(secondList); return list;}I can change the objects parameter to be List and then avoid the possible multiple enumeration but then I don't get the highest object that I can handle.Another thing that I can do is to convert the IEnumerable to List at the beginning of the method: public List<object> Foo(IEnumerable<object> objects) { var objectList = objects.ToList(); // ... }But this is just awkward.What would you do in this scenario? 解决方案 The problem with taking IEnumerable as a parameter is that it tells callers "I wish to enumerate this". It doesn't tell them how many times you wish to enumerate. I can change the objects parameter to be List and then avoid the possible multiple enumeration but then I don't get the highest object that I can handle.The goal of taking the highest object is noble, but it leaves room for too many assumptions. Do you really want someone to pass a LINQ to SQL query to this method, only for you to enumerate it twice (getting potentially different results each time?)The semantic missing here is that a caller, who perhaps doesn't take time to read the details of the method, may assume you only iterate once - so they pass you an expensive object. Your method signature doesn't indicate either way.By changing the method signature to IList/ICollection, you will at least make it clearer to the caller what your expectations are, and they can avoid costly mistakes.Otherwise, most developers looking at the method might assume you only iterate once. If taking an IEnumerable is so important, you should consider doing the .ToList() at the start of the method.It's a shame .NET doesn't have an interface that is IEnumerable + Count + Indexer, without Add/Remove etc. methods, which is what I suspect would solve this problem. 这篇关于处理可能对 IEnumerable 进行多次枚举的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 13:20