{"record":{"id":"77544e2b214ffed3","repo":"dotnet/wpf","slug":"sr-enumeratorcollectiondisposed-columndefinition","errorCode":null,"errorMessage":"SR.EnumeratorCollectionDisposed","messagePattern":"SR\\.EnumeratorCollectionDisposed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":920,"sourceCode":"\n            /// <summary>\n            ///     <see cref=\"IDisposable.Dispose\"/>\n            /// </summary>\n            public void Dispose()\n            {\n                _currentElement = null;\n            }\n\n            /// <summary>\n            ///     Validates that\n            ///     enumerator is not disposed;\n            ///     enumerator is still in sync with collection;\n            /// </summary>\n            private void PrivateValidate()\n            {\n                if (_currentElement == null)\n                {\n                    throw new InvalidOperationException(SR.EnumeratorCollectionDisposed);\n                }\n                if (_version != _collection._version)\n                {\n                    throw new InvalidOperationException(SR.EnumeratorVersionChanged);\n                }\n            }\n\n            private ColumnDefinitionCollection _collection;              //  the collection to be enumerated\n            private int _index;                         //  current element index\n            private int _version;                       //  the snapshot of collection's version at the time of creation\n            private object _currentElement;             //  multipurpose:\n                                                        //  points to the collection object when enumerator is either before start or after end\n                                                        //  points to the current element while in the process of enumeration\n                                                        //  is null if disposed\n        }\n\n        #endregion Private Structures Classes\n    }","sourceCodeStart":902,"sourceCodeEnd":938,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L902-L938","documentation":"The enumerator was invalidated because the underlying collection state is gone (_currentElement == null), which happens after the collection was disposed/cleared out from under the enumerator. PrivateValidate also throws EnumeratorVersionChanged if the collection was merely modified.","triggerScenarios":"Calling MoveNext() or Reset() on an enumerator after its owning ColumnDefinitionCollection has been disposed/invalidated (e.g. the Grid tore down its definitions).","commonSituations":"Holding an enumerator across a Grid teardown; long-lived enumerators cached in fields while the collection is reset; enumerating while the collection is cleared on another thread.","solutions":["Re-acquire the enumerator via GetEnumerator() after the collection changes","Do not cache enumerators across collection mutations","Enumerate a snapshot (e.g. grid.ColumnDefinitions.ToArray()) if the collection may change"],"exampleFix":"// before\n_cachedEnumerator = grid.ColumnDefinitions.GetEnumerator();\n...\nwhile (_cachedEnumerator.MoveNext()) { }\n// after\nforeach (var col in grid.ColumnDefinitions.ToArray()) { }","handlingStrategy":"try-catch","validationCode":"var snapshot = grid.ColumnDefinitions.ToArray(); foreach (var col in snapshot) { }","typeGuard":null,"tryCatchPattern":"try { while (e.MoveNext()) { } } catch (InvalidOperationException) { e = grid.ColumnDefinitions.GetEnumerator(); }","preventionTips":["Don't cache enumerators across collection changes","Snapshot the collection before long operations","Re-get enumerators after Clear/teardown"],"tags":["wpf","enumerator","invalid-operation","collection-modified"],"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-22T01:17:13.364Z"}