{"record":{"id":"69dfb790a2a42a54","repo":"dotnet/wpf","slug":"throw-new-system-invalidoperationexception-sr-enumerator","errorCode":null,"errorMessage":"throw new System.InvalidOperationException(SR.Enumerator_VerifyContext);","messagePattern":"throw new System\\.InvalidOperationException\\(SR\\.Enumerator_VerifyContext\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/WeakReferenceEnumerator.cs","lineNumber":37,"sourceCode":"    internal struct WeakReferenceListEnumerator : IEnumerator\n    {\n        public WeakReferenceListEnumerator( ArrayList List)\n        {\n            _i = 0;\n            _List = List;\n            _StrongReference = null;\n        }\n\n        object IEnumerator.Current\n        {  get{ return Current; } }\n        \n        public object Current\n        {\n            get\n            {\n                if( null == _StrongReference )\n                {\n                    throw new System.InvalidOperationException(SR.Enumerator_VerifyContext);\n                }\n                return _StrongReference;\n            }\n        }\n\n        public bool MoveNext()\n        {\n            object obj=null;\n            while( _i < _List.Count )\n            {\n                WeakReference weakRef = (WeakReference) _List[ _i++ ];\n                obj = weakRef.Target;\n                if(null != obj)\n                    break;\n            }\n            _StrongReference = obj;\n            return (null != obj);\n        }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/WeakReferenceEnumerator.cs#L19-L55","documentation":"WeakReferenceListEnumerator.Current throws InvalidOperationException(SR.Enumerator_VerifyContext) when the strong reference has not been established, meaning MoveNext was not called before reading Current (or the enumerator was used out of order). It enforces the standard IEnumerator protocol.","triggerScenarios":"Accessing the Current property of WeakReferenceListEnumerator immediately after creation or after MoveNext returned false, without a successful MoveNext establishing _StrongReference.","commonSituations":"Manual foreach-equivalent loops over weak-reference lists where MoveNext's return value is ignored; resuming an exhausted enumerator; sharing one enumerator across threads.","solutions":["Call MoveNext() and check it returns true before reading Current","Restructure as foreach or a while(en.MoveNext()) loop","Do not read Current again after the loop completes","Create a fresh enumerator instead of resetting/reusing a finished one"],"exampleFix":"// before\nvar e = list.GetEnumerator();\nobject v = e.Current;\n// after\nvar e = list.GetEnumerator();\nif (e.MoveNext()) { object v = e.Current; }","handlingStrategy":"try-catch","validationCode":"// protocol check: only read Current after MoveNext()==true\n// bool canRead = enumerator.MoveNext();","typeGuard":"static bool TryCurrent(System.Collections.IEnumerator e, out object v) {\n    v = null;\n    if (!e.MoveNext()) return false;\n    v = e.Current; return true;\n}","tryCatchPattern":"try { v = e.Current; }\ncatch (InvalidOperationException) { /* MoveNext not called/failed: reset loop */ }","preventionTips":["Always gate Current on MoveNext()'s return value","Prefer foreach over manual enumerator loops","Never reuse exhausted enumerators"],"tags":["wpf","enumerator","invalid-operation","sequence"],"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"}