skip to content

A C# custom operator calls `source.Max()` and then loops over `source` to scale each reading; why does it print 104% on a live sensor feed, and how do you fix it?

level: seniorimportance: should knowfreq 40%

answer

  1. two calls, two enumerations
  2. the iterator body runs again
  3. single-use and endless sources
  4. the CA1851 analyzer rule
  5. one pass, or buffer once

basics

~20 s

Max() and the foreach each enumerate the source, so a live feed is read twice and the second read returns newer, higher readings than the peak. Read the source once: keep one enumerator, or copy it into an array and compute over the copy.

solid answer

~50 s

`source.Max()` is one complete enumeration and the `foreach` is a second one. If the source is a `yield`-based feed, its body runs again from the top, so the second pass reads newer readings that are higher than the peak taken from the first, hence 104%. Other sources fail differently: a single-use source returns only what the first pass left, an endless one never lets `Max()` return, and an expensive one costs twice. The CA1851 analyzer rule, "Possible multiple enumerations of `IEnumerable` collection", flags this pattern but is not enabled by default in .NET 10. The fix is to enumerate once: if the operator only needs per-element state, use one enumerator; if it needs a whole-sequence value like the peak, copy the source with `ToArray()` once, compute over the copy, and document the operator as buffering.

code

csharp · 23 lines
csharp
foreach (int pct in LiveFeed.NextBatch().ScaleToPeak())
    Console.WriteLine($"{pct}%");

static class LiveFeed
{
    static int s_next = 20;                  // the sensor keeps moving between reads
    public static IEnumerable<int> NextBatch()
    {
        Console.WriteLine("  connect");
        for (int i = 0; i < 3; i++)
            yield return s_next++;
    }
}

static class SensorOps
{
    public static IEnumerable<int> ScaleToPeak(this IEnumerable<int> source)
    {
        int peak = source.Max();             // enumeration 1
        foreach (int value in source)        // enumeration 2: a fresh read
            yield return value * 100 / peak;
    }
}

go deeper

for a junior

Remember that each LINQ call such as Max or Count and each foreach starts a new enumeration of an IEnumerable<T>.

for a middle

Explain what a second enumeration does to an iterator, a live feed, a single-use source and an endless one, using the 104% trace.

for a senior

Fix it by choosing between one pass with streaming state and one deliberate buffer, enable CA1851, and document which kind the operator is.

for a principal

Decide whether shared operators accept IEnumerable<T> or IReadOnlyList<T>, and make CA1851 part of the analyzer baseline for libraries that take sequences.

## Why one IEnumerable<T> can give two different answers An `IEnumerable<T>` is not a collection; it is a promise that `GetEnumerator()` will hand out an enumerator. Every LINQ call that has to read it, such as `Max()`, `Any()` or `ToList()`, and every `foreach` asks for a **new enumeration** (`Count()` and a few others take a shortcut when the source is a collection that already knows its size). Whether two enumerations see the same elements depends entirely on the source: | Source | Second enumeration | |---|---| | `List<T>` or array | same elements, cheap | | `yield`-based iterator | the method body runs again from the top | | live feed behind an iterator | reconnects and reads newer data | | single-use source (a shared reader or network stream) | gets only what the first pass did not consume | | endless iterator | the first full pass, such as `Max()`, never ends | An operator author cannot know which of these a caller will pass, so an operator that enumerates `source` twice is correct only for some callers. ## Tracing the 104% In the example on this page the feed is an iterator that prints `connect` and reads three values from a sensor that keeps moving. `ScaleToPeak` first calls `source.Max()`: that runs the iterator body, reads 20, 21 and 22, and finds a peak of 22. Then its `foreach` runs the body **again**: `connect` prints a second time, and the readings are now 23, 24 and 25, which scale to 104%, 109% and 113% of the stale peak. No exception, no warning in a default build, just wrong numbers. The same shape breaks differently elsewhere: - On an **endless** feed, `Max()` never returns, so the operator never yields. - On a **single-use** source, the `foreach` finds nothing left and yields nothing. - The common `if (!source.Any()) yield break;` guard calls `MoveNext` once, so on a single-use source it **swallows the first element**: a check-then-loop over 20, 21, 22 prints only 21 and 22. - On an **expensive** source, such as a file parse or a remote query, every pass pays the full cost again. ## Fixes, from best to most costly 1. **One pass with per-element state.** If the operator only needs the previous element, a running total or the peak so far, keep one enumerator and update state as you go. Replace an `Any()` guard with `using var e = source.GetEnumerator(); if (!e.MoveNext()) yield break;` and continue from `e.Current`. 2. **Buffer once, on purpose.** When the result genuinely needs a whole-sequence value (scaling to the peak of the batch), copy the source once and work on the copy: ```csharp int[] buffer = source.ToArray(); // the only enumeration of source if (buffer.Length == 0) yield break; int peak = buffer.Max(); foreach (int value in buffer) yield return value * 100 / peak; ``` This prints 90%, 95% and 100% for 20, 21 and 22. It is now a buffering operator: correct on single-use sources, still unusable on an endless one, which its documentation should say. 3. **Change the contract.** Take `IReadOnlyList<int>` instead of `IEnumerable<int>`, so the caller decides when to materialise and the operator may index freely. `TryGetNonEnumeratedCount` helps only with counts: it returns `true` without enumerating when the source can report its size, such as a `List<T>`, and `false` for a `yield`-based iterator, so an operator still needs the one-pass fallback. ## Making the build catch it **CA1851**, "Possible multiple enumerations of `IEnumerable` collection", was introduced with the .NET 7 analyzers and is **not enabled by default in .NET 10**. Turn it on in `.editorconfig`: ```ini [*.cs] dotnet_diagnostic.CA1851.severity = warning ``` With that line, the SDK reports CA1851 on both `source.Max()` and `foreach (int value in source)` in `ScaleToPeak`. The rule's name says "possible": it sees only the `IEnumerable<T>` parameter type and cannot know whether callers pass a `List<T>` or an iterator, so treat each hit as a question about what callers may pass, not as proof of a bug. ## Review checklist - Count how many times the operator body touches `source`; more than once is a smell. - Ask what happens on an endless, a single-use and an expensive source. - Prefer streaming state; buffer only when the result needs the whole sequence, and say so.

  • How do you make the build report this pattern?
    Enable CA1851 in `.editorconfig` with `dotnet_diagnostic.CA1851.severity = warning`. The rule shipped with the .NET 7 analyzers and is not enabled by default in .NET 10. It then flags both `source.Max()` and the `foreach` over the same parameter as possible multiple enumerations.
  • Why does `if (!source.Any()) yield break;` before a `foreach` lose a reading on a single-use source?
    `Any()` gets an enumerator and calls `MoveNext` once to see whether an element exists, which consumes that element from a single-use source. The `foreach` then starts at the second one. Use one enumerator: call `MoveNext` once yourself and keep processing from `e.Current`.
  • Can `TryGetNonEnumeratedCount` replace the first pass?
    Only for a count, and only sometimes. It returns `true` without enumerating when the source can report its size cheaply, such as a `List<T>`, and `false` for a `yield`-based iterator. The operator still needs a single-pass path for the `false` case.

  • Max() caches the elements, so the foreach afterwards reads them for free
  • Every IEnumerable<T> returns the same elements each time it is enumerated
  • CA1851 is on by default, so the build would already warn
  • Calling ToArray inside the operator keeps it safe on an endless feed
  • Any() only peeks at the source and consumes nothing