ReSharper 2024.3 Help

Code inspection: Possible multiple enumeration

Consider the following code snippet:

IEnumerable<string> names = GetNames(); foreach (var name in names) Console.WriteLine("Found " + name); var allNames = new StringBuilder(); foreach (var name in names) allNames.Append(name + " ");

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:

List<string> names = GetNames().ToList();

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:

public void Foo(IEnumerable<string> values) { ThrowIfNull(values, nameof(values)); var x = values.ToList(); // Possible multiple enumeration of IEnumerable } public static void ThrowIfNull<T>(T value, string name) where T : class { // custom check for null but no enumeration }

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:

public void Foo(IEnumerable<string> values) { ThrowIfNull(values, nameof(values)); var x = values.ToList(); // No warnings about multiple enumeration } public static void ThrowIfNull<T>([NoEnumeration] T value, string name) where T : class { // custom check for null but no enumeration }
Last modified: 11 February 2024