dotnet/wpf · error

SR.EnumeratorNotStarted

Error message

SR.EnumeratorNotStarted

What it means

The enumerator's Current property throws InvalidOperationException with SR.EnumeratorNotStarted when Current is read before the first MoveNext call (_index == -1). The sentinel _currentElement == _collection marks Current as not yet valid, and _index distinguishes 'not started' from 'past the end'.

Solutions

  1. Call MoveNext() and check it returns true before reading Current
  2. Use a foreach loop, which never reads Current before MoveNext succeeds
  3. If Current may be read at any time, guard with a position check or snapshot the collection

Example fix

// before
var e = collection.GetEnumerator();
var first = ((IEnumerator)e).Current;
// after
var e = collection.GetEnumerator();
if (e.MoveNext()) { var first = e.Current; }
Defensive patterns

Strategy: try-catch

Try / catch

object current = null; if (enumerator.MoveNext()) { current = enumerator.Current; } else { /* handle empty/start state */ }

Prevention

When it happens

Trigger: Accessing the enumerator's Current property (or IEnumerable<T>.Current via an interface) immediately after calling GetEnumerator(), before any MoveNext()/MoveNext() returning true.

Common situations: Hand-rolled iteration that reads Current once before the loop, a do/while loop that touches Current before advancing, or DI/logging code that inspects the 'current' item at enumerator creation.

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/9d519c9c7c936f49. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs:343

                    return (true);
                }
                else
                {
                    _currentElement = _collection;
                    _index = _collection.Size;
                    return (false);
                }
            }

            public TItem Current
            {
                get
                {
                    if (_currentElement == _collection)
                    {
                        if (_index == -1)
                        {
                            throw new InvalidOperationException(SR.EnumeratorNotStarted);
                        }
                        else
                        {
                            throw new InvalidOperationException(SR.EnumeratorReachedEnd);
                        }
                    }
                    return (TItem)_currentElement;
                }
            }

            /// <summary>
            ///     <see cref="IEnumerator.Current"/>
            /// </summary>
            object IEnumerator.Current
            {
                get
                {
                    return this.Current;

View on GitHub (pinned to 81131a70a4)