在我的代码中,我需要使用IEnumerable<>几次,导致ReSharper错误“可能的IEnumerable多重枚举”。
示例代码:
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;
}
我可以将对象参数改为List,然后避免可能的多重枚举,但这样我就得不到我能处理的最高的对象。
我能做的另一件事是在方法的开头将IEnumerable转换为List:
public List<object> Foo(IEnumerable<object> objects)
{
var objectList = objects.ToList();
// ...
}
但这太尴尬了。
在这种情况下你会怎么做?
在这种情况下,我通常用IEnumerable和IList重载我的方法。
public static IEnumerable<T> Method<T>( this IList<T> source ){... }
public static IEnumerable<T> Method<T>( this IEnumerable<T> source )
{
/*input checks on source parameter here*/
return Method( source.ToList() );
}
我注意在方法的摘要注释中解释了调用IEnumerable将执行. tolist()。
如果多个操作被连接在一起,程序员可以在更高的级别上选择. tolist(),然后调用IList重载或让IEnumerable重载来处理。
如果您的数据总是可重复的,也许就不用担心了。但是,你也可以展开它——如果传入的数据可能很大(例如,从磁盘/网络读取),这特别有用:
if(objects == null) throw new ArgumentException();
using(var iter = objects.GetEnumerator()) {
if(!iter.MoveNext()) throw new ArgumentException();
var firstObject = iter.Current;
var list = DoSomeThing(firstObject);
while(iter.MoveNext()) {
list.Add(DoSomeThingElse(iter.Current));
}
return list;
}
注意,我稍微改变了DoSomethingElse的语义,但这主要是为了显示展开的用法。例如,您可以重新包装迭代器。你也可以把它变成一个迭代器块,这样很好;然后就没有列表了——你会在得到它们的时候就返回它们,而不是添加到要返回的列表中。
.NET 6/ c# 10
. .除此之外,您可以尝试确定序列中的元素数量,而无需使用Enumerable强制枚举。TryGetNonEnumeratedCount(IEnumerable, Int32)方法。
如果source的计数可以在没有枚举的情况下确定,则此方法返回true;否则,假的。因此,您可以检查是否需要进一步实现。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
IEnumerable<int> arrayOne = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var canGetCountDirectly = arrayOne.TryGetNonEnumeratedCount(out int theCount);
Console.WriteLine($"Count can be returned directly = {canGetCountDirectly}");
Console.WriteLine($"Count = {theCount}");
}
}