In my code in need to use an IEnumerable<>
several times thus get 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;
}
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. 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.