dotnet/wpf · error · InvalidOperationException

SR.Enumerator_NotStarted

Error message

SR.Enumerator_NotStarted

What it means

DrawingCollection's Enumerator.Current throws InvalidOperationException (SR.Enumerator_NotStarted) when accessed while _index == -1, i.e. before the first MoveNext call. Current is only valid between a successful MoveNext and the next step/past-the-end.

Solutions

  1. Call MoveNext() and check its return value before reading Current.
  2. Prefer foreach, which handles the protocol for you.
  3. If lazily reading current items, initialize with if (e.MoveNext()) { var d = e.Current; }.

Example fix

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

Strategy: validation

Validate before calling

if (e.MoveNext()) { var current = e.Current; }

Type guard

null

Try / catch

try { var c = e.Current; } catch (InvalidOperationException) { /* enumerator not started: call MoveNext first */ }

Prevention

When it happens

Trigger: Reading enumerator.Current (or accessing Current via manual enumerator code) immediately after GetEnumerator() without calling MoveNext().

Common situations: Hand-rolled enumerator loops that read Current before stepping; generic helper code assuming Current has a default value like .NET's IEnumerator contract differences.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs:884

            /// <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 Drawing 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 Drawing _current;
            private DrawingCollection _list;
            private uint _version;
            private int _index;
            #endregion
        }

View on GitHub (pinned to 81131a70a4)