Code inspection: Possible multiple enumeration
Consider the following code snippet:
Assuming that GetNames()
returns an IEnumerable<string>
, we are, effectively, doing extra work by enumerating this collection twice in the two foreach
statements.
It could get even worse if GetNames()
results in a database query, in which case you could end up getting different values in both foreach
loops if some other process changes the database between the two calls.
This kind of problem can be easily fixed — force the enumeration at the point of variable initialization by converting the sequence to an array or a list, for example:
The rest of your code can stay the same, because both array and list types implement the IEnumerable
interface.
False positives
Sometimes, this inspection may render false positives if the IEnumerable object is passed to some method before being enumerated. For example:
In this case, ReSharper presumes that the method will do additional enumeration, which is true in most cases. If the method doesn't actually enumerate the IEnumerable object, you can mark the corresponding parameter with the [NoEnumerationAttribute] to prevent the false positive: