{"record":{"id":"d6cea4808a3600dd","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-enumeratornotstarted","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.EnumeratorNotStarted);","messagePattern":"throw new InvalidOperationException\\(SR\\.EnumeratorNotStarted\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs","lineNumber":1554,"sourceCode":"                        _current = null;\n                        _done = true;\n                        isCurrentInView = false;\n                        break;\n                    }\n                }\n                return isCurrentInView;\n            }\n\n            public object Current\n            {\n                get\n                {\n                    // the spec for ICollectionView.CurrentItem says:\n                    // InvalidOperationException: The enumerator is positioned before the first element of the collection or after the last element.\n                    if (_index < 0)\n                    {\n                        // ICollectionView.CurrentItem is documented to throw this exception\n                        throw new InvalidOperationException(SR.EnumeratorNotStarted);\n                    }\n                    if (_done)\n                    {\n                        // ICollectionView.CurrentItem is documented to throw this exception\n                        throw new InvalidOperationException(SR.EnumeratorReachedEnd);\n                    }\n\n                    return _current;\n                }\n            }\n\n            public void Reset()\n            {\n                CheckVersion();\n                _index = -1;\n                _current = null;\n                DisposeContainerEnumerator();\n                _done = false;","sourceCodeStart":1536,"sourceCodeEnd":1572,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs#L1536-L1572","documentation":"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.","triggerScenarios":"Accessing Current on a fresh CompositeCollectionView enumerator (or the view's CurrentItem while positioned before the first element) without a preceding MoveNext/MoveCurrentToFirst.","commonSituations":"foreach-like manual enumeration that reads Current before looping; currency left at BeforeFirst after a view reset and code reading CurrentItem unconditionally.","solutions":["Call MoveNext() (or MoveCurrentToFirst()) before reading Current/CurrentItem.","Check the view's IsCurrentBeforeFirst / IsCurrentAfterLast before accessing CurrentItem.","Reset currency explicitly with MoveCurrentToFirst or MoveCurrentTo after a Reset notification."],"exampleFix":"// before\nvar it = compositeCollectionView.GetEnumerator();\nvar current = it.Current;\n\n// after\nvar it = compositeCollectionView.GetEnumerator();\nif (it.MoveNext())\n{\n    var current = it.Current;\n}","handlingStrategy":"validation","validationCode":"bool canReadCurrent = view.CurrentPosition >= 0 && !view.IsCurrentBeforeFirst && !view.IsCurrentAfterLast;","typeGuard":"static bool CanReadCurrent(IEnumerator it) => it.MoveNext || CurrentWasStarted(it);","tryCatchPattern":"try { var current = it.Current; }\ncatch (InvalidOperationException) { /* enumeration not started; call MoveNext first */ }","preventionTips":["Always call MoveNext before the first Current read.","Check IsCurrentBeforeFirst before CurrentItem access.","Reset currency after any Reset notification."],"tags":["wpf","enumerator","collection-view","current"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}