dotnet/wpf · error · InvalidOperationException

throw new…

Error message

throw new InvalidOperationException(SR.EnumeratorNotStarted);

What it means

The enumerator's Current getter throws InvalidOperationException(SR.EnumeratorNotStarted) when Current is read before MoveNext was ever called (_index < 0). This matches the ICollectionView/IEnumerator contract that Current is invalid before enumeration starts.

Solutions

  1. Call MoveNext() (or MoveCurrentToFirst()) before reading Current/CurrentItem.
  2. Check the view's IsCurrentBeforeFirst / IsCurrentAfterLast before accessing CurrentItem.
  3. Reset currency explicitly with MoveCurrentToFirst or MoveCurrentTo after a Reset notification.

Example fix

// before
var it = compositeCollectionView.GetEnumerator();
var current = it.Current;

// after
var it = compositeCollectionView.GetEnumerator();
if (it.MoveNext())
{
    var current = it.Current;
}
Defensive patterns

Strategy: validation

Validate before calling

bool canReadCurrent = view.CurrentPosition >= 0 && !view.IsCurrentBeforeFirst && !view.IsCurrentAfterLast;

Type guard

static bool CanReadCurrent(IEnumerator it) => it.MoveNext || CurrentWasStarted(it);

Try / catch

try { var current = it.Current; }
catch (InvalidOperationException) { /* enumeration not started; call MoveNext first */ }

Prevention

When it happens

Trigger: Accessing Current on a fresh CompositeCollectionView enumerator (or the view's CurrentItem while positioned before the first element) without a preceding MoveNext/MoveCurrentToFirst.

Common situations: foreach-like manual enumeration that reads Current before looping; currency left at BeforeFirst after a view reset and code reading CurrentItem unconditionally.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs:1554

                        _current = null;
                        _done = true;
                        isCurrentInView = false;
                        break;
                    }
                }
                return isCurrentInView;
            }

            public object Current
            {
                get
                {
                    // the spec for ICollectionView.CurrentItem says:
                    // InvalidOperationException: The enumerator is positioned before the first element of the collection or after the last element.
                    if (_index < 0)
                    {
                        // ICollectionView.CurrentItem is documented to throw this exception
                        throw new InvalidOperationException(SR.EnumeratorNotStarted);
                    }
                    if (_done)
                    {
                        // ICollectionView.CurrentItem is documented to throw this exception
                        throw new InvalidOperationException(SR.EnumeratorReachedEnd);
                    }

                    return _current;
                }
            }

            public void Reset()
            {
                CheckVersion();
                _index = -1;
                _current = null;
                DisposeContainerEnumerator();
                _done = false;

View on GitHub (pinned to 81131a70a4)