{"record":{"id":"c3608a53b8fa72d5","repo":"dotnet/wpf","slug":"sr-enumeratorcollectiondisposed","errorCode":null,"errorMessage":"SR.EnumeratorCollectionDisposed","messagePattern":"SR\\.EnumeratorCollectionDisposed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/HostedElements.cs","lineNumber":161,"sourceCode":"        {\n            get { return this.Current; }\n        }\n\n        void IEnumerator.Reset()\n        {\n            throw new NotImplementedException();\n        }\n\n        public IInputElement Current\n        {\n            get\n            {\n                // HostedElements must throw exception if Current property is incorrectly accessed\n                if (_textSegments == null)\n                {\n                    // Collection was modified \n                    // IEnumerator.Current is documented to throw this exception\n                    throw new InvalidOperationException(SR.EnumeratorCollectionDisposed);\n                }\n\n                if (_currentPosition == null)\n                {\n                    // Enumerator not started. Call MoveNext to see if we can move ahead\n                    // IEnumerator.Current is documented to throw this exception\n                    throw new InvalidOperationException(SR.EnumeratorNotStarted);\n                }\n\n                IInputElement currentElement = null;\n                switch (_currentPosition.GetPointerContext(LogicalDirection.Forward))\n                {\n                    case TextPointerContext.ElementStart:\n                        Debug.Assert(_currentPosition.GetAdjacentElementFromOuterPosition(LogicalDirection.Forward) is IInputElement);\n                        currentElement = _currentPosition.GetAdjacentElementFromOuterPosition(LogicalDirection.Forward);\n                        break;\n                    case TextPointerContext.EmbeddedElement:\n                        Debug.Assert(_currentPosition.GetAdjacentElement(LogicalDirection.Forward) is IInputElement);","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/HostedElements.cs#L143-L179","documentation":"The HostedElements enumerator's Current getter throws InvalidOperationException (SR.EnumeratorCollectionDisposed) when the enumerator has been disposed/reset (_textSegments == null). Per the IEnumerator contract, accessing Current after the collection state is gone is an error; the comment notes this also covers 'collection was modified'.","triggerScenarios":"Reading Current after calling Reset (which effectively disposes the enumerator's state) or otherwise after the enumerator's internal text-segment list was cleared — including from a finally/deferred block or a cached enumerator kept after iteration ended.","commonSituations":"Storing the enumerator long-term and reading Current later; generic iteration helpers that touch Current after Reset or after the document content changed, invalidating the enumerator.","solutions":["Do not read Current after Reset or after the enumerator finished; call MoveNext first and check its result before accessing Current.","Acquire a new enumerator from HostedElements when you need to iterate again.","Avoid caching enumerators of FlowDocument content across content edits."],"exampleFix":"// before\nenumerator.Reset();\nvar el = enumerator.Current; // throws\n// after\nenumerator = hostedElements.GetEnumerator();\nif (enumerator.MoveNext()) { var el = enumerator.Current; }","handlingStrategy":"validation","validationCode":"if (enumerator.MoveNext())\n{\n    var current = enumerator.Current;\n}\n// Only read Current after a successful MoveNext; never after Reset.","typeGuard":"static bool TryReadCurrent(IEnumerator e, out object value)\n{ value = null; try { value = e.Current; return true; } catch (InvalidOperationException) { return false; } }","tryCatchPattern":"try { var el = enumerator.Current; }\ncatch (InvalidOperationException) { enumerator = hostedElements.GetEnumerator(); }","preventionTips":["Read Current only after a true MoveNext result","Re-create enumerators after Reset or content edits","Do not cache HostedElements enumerators across document changes"],"tags":["wpf","enumerator","disposed","invalid-state"],"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"}