dotnet/wpf · error · InvalidOperationException

SR.Enumerator_NotStarted

Error message

SR.Enumerator_NotStarted

What it means

The DoubleCollection enumerator's Current property throws InvalidOperationException with SR.Enumerator_NotStarted when accessed before the first MoveNext call. _index is still -1 (positioned before the first element), so there is no valid current element to return.

Solutions

  1. Call MoveNext() at least once before reading Current.
  2. Structure iteration as while (enumerator.MoveNext()) { use enumerator.Current; }.
  3. Use a foreach loop, which always calls MoveNext before exposing the element.

Example fix

// before
var e = coll.GetEnumerator();
var d = e.Current; // InvalidOperationException: Enumerator_NotStarted
// after
var e = coll.GetEnumerator();
while (e.MoveNext()) { var d = e.Current; }
Defensive patterns

Strategy: type-guard

Validate before calling

// track MoveNext state before touching Current
bool started = false;
// call MoveNext() at least once before reading Current

Type guard

static T? SafeCurrent<T>(IEnumerator<T> e) where T : struct => e.MoveNext() ? e.Current : default; // wrap with started-flag logic

Try / catch

try { var d = e.Current; }
catch (InvalidOperationException ex) when (ex.Message.Contains("Enumerator_NotStarted"))
{ if (e.MoveNext()) { var d = e.Current; } }

Prevention

When it happens

Trigger: Getting an enumerator from a DoubleCollection and reading Current (or using foreach's current variable manually) before calling MoveNext() the first time.

Common situations: Manual IEnumerator loops where Current is read before MoveNext; enumerator positioned and inspected in a debugger/property evaluator that touches Current; hand-rolled iteration code skipping the initial MoveNext.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6548d620b6b86efe. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs:867

            /// <summary>
            /// Current element
            ///
            /// The behavior of IEnumerable&lt;T>.Current is undefined
            /// before the first MoveNext and after we have walked
            /// off the end of the list. However, the IEnumerable.Current
            /// contract requires that we throw exceptions
            /// </summary>
            public double Current
            {
                get
                {
                    if (_index > -1)
                    {
                        return _current;
                    }
                    else if (_index == -1)
                    {
                        throw new InvalidOperationException(SR.Enumerator_NotStarted);
                    }
                    else
                    {
                        Debug.Assert(_index == -2, "expected -2, got " + _index + "\n");
                        throw new InvalidOperationException(SR.Enumerator_ReachedEnd);
                    }
                }
            }

            #endregion

            #region Data
            private double _current;
            private DoubleCollection _list;
            private uint _version;
            private int _index;
            #endregion
        }

View on GitHub (pinned to 81131a70a4)