{"record":{"id":"078b295782435b42","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-indexedenumerable","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.EnumeratorVersionChanged);","messagePattern":"throw new InvalidOperationException\\(SR\\.EnumeratorVersionChanged\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs","lineNumber":722,"sourceCode":"        //------------------------------------------------------\n        //\n        //  Private Types\n        //\n        //------------------------------------------------------\n        private class FilteredEnumerator : IEnumerator, IDisposable\n        {\n            public FilteredEnumerator(IndexedEnumerable indexedEnumerable, IEnumerable enumerable, Predicate<object> filterCallback)\n            {\n                _enumerable = enumerable;\n                _enumerator = _enumerable.GetEnumerator();\n                _filterCallback = filterCallback;\n                _indexedEnumerable = indexedEnumerable;\n            }\n\n            void IEnumerator.Reset()\n            {\n                if (_indexedEnumerable._enumerable == null)\n                    throw new InvalidOperationException(SR.EnumeratorVersionChanged);\n\n                Dispose();\n                _enumerator = _enumerable.GetEnumerator();\n            }\n\n            bool IEnumerator.MoveNext()\n            {\n                bool returnValue;\n\n                if (_indexedEnumerable._enumerable == null)\n                    throw new InvalidOperationException(SR.EnumeratorVersionChanged);\n\n                if (_filterCallback == null)\n                {\n                    returnValue = _enumerator.MoveNext();\n                }\n                else\n                {","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs#L704-L740","documentation":"IndexedEnumerable's IEnumerator.Reset() requires the owning IndexedEnumerable to still reference a live enumerable (_enumerable != null). When the parent enumerable was replaced or cleared (e.g. the view re-created the IndexedEnumerable after a collection change), Reset() treats the enumerator as orphaned and throws InvalidOperationException with EnumeratorVersionChanged.","triggerScenarios":"Calling IEnumerator.Reset() (explicit interface implementation) on an enumerator obtained from an IndexedEnumerable after the source enumerable was detached — typically following a collection Refresh or rebind that invalidated the enumerator.","commonSituations":"Holding an IEnumerator across a CollectionView.Refresh() or ItemsSource change and calling Reset() to restart iteration; long-lived enumerators stored in helper classes while the view rebinds.","solutions":["Discard the stale enumerator and request a new one via GetEnumerator() instead of calling Reset().","Re-query the view/enumerable after any Refresh or rebind before resuming iteration.","Restructure iteration into a method that creates a fresh enumerator on each pass.","Catch InvalidOperationException and fall back to re-enumeration with a new enumerator."],"exampleFix":"// before\nenumerator.Reset(); // stale after refresh\n// after\nenumerator = indexedEnumerable.GetEnumerator(); // fresh enumerator","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { enumerator.Reset(); }\ncatch (InvalidOperationException) { enumerator = enumerable.GetEnumerator(); }","preventionTips":["Treat enumerators as invalid after Refresh/rebind; create new ones.","Avoid calling Reset() on WPF collection-view enumerators.","Scope enumerators to a single iteration pass.","Re-acquire enumerators after any ItemsSource change."],"tags":["wpf","enumerator","invalidated","collection-view"],"backgroundTag":"collection-modified-during-enumeration","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"}