Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erroneous Resharper multiple enumeration warning?

This code:

IEnumerable<IEnumerable<int>> numbas = new[] {new[] {0, 1}, new[] {2}, new[] {3, 4, 5}};
var flattened = numbas.SelectMany(a => a);

extracts a single flattened enumerable list of numbers from several sources. Resharper warns that it's possible that a (the second one) is being enumerated multiple times -- but this is silly; each source is being enumerated once only. Yes, the symbol a is going to be enumerated multiple times, but there will be a different source under it each time.

Did I miss something, or is this an erroneous warning coming out of Resharper?

like image 515
CSJ Avatar asked May 22 '14 19:05

CSJ


Video Answer


1 Answers

Yes, this is an erroneous warning. You can see if you take a look at the implementation of SelectMany - there's only one enumeration of the nested element:

foreach (TSource element in source) {
    foreach (TResult subElement in selector(element)) {
        yield return subElement;
    }
}

Here's the YouTrack issue for this: http://youtrack.jetbrains.com/issue/RSRP-413613

like image 129
citizenmatt Avatar answered Sep 23 '22 06:09

citizenmatt