{"record":{"id":"cf0025bc963791e2","repo":"dotnet/wpf","slug":"sr-timing-enumeratoroutofrange","errorCode":null,"errorMessage":"SR.Timing_EnumeratorOutOfRange","messagePattern":"SR\\.Timing_EnumeratorOutOfRange","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/TimelineClockCollection.cs","lineNumber":344,"sourceCode":"                // The enumerator doesn't do much, so we don't have to do\n                // anything to dispose it.\n            }\n\n            #endregion // IDisposable interface\n\n            #region IEnumerator interface\n\n            /// <summary>\n            /// Gets the current element in the collection.\n            /// </summary>\n            /// <value>\n            /// The current element in the collection.\n            /// </value>\n            Clock IEnumerator<Clock>.Current\n             {\n                get\n                {\n                    throw new InvalidOperationException(SR.Timing_EnumeratorOutOfRange);\n                }\n            }\n\n            #region IEnumerator Members\n\n            object System.Collections.IEnumerator.Current\n            {\n                get\n                {\n                    return ((IEnumerator<Clock>)this).Current;\n                }\n            }\n\n            void System.Collections.IEnumerator.Reset()\n            {\n                throw new NotImplementedException();\n            }\n","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/TimelineClockCollection.cs#L326-L362","documentation":"The nested ClockEnumerator's IEnumerator<Clock>.Current throws InvalidOperationException with SR.Timing_EnumeratorOutOfRange when accessed while the enumerator is positioned before the first element (before any MoveNext) or after the last element (past the end). This is the standard CLR enumerator contract, surfaced with WPF's timing resource string.","triggerScenarios":"Reading Current before calling MoveNext (e.g. var c = enumerator.Current;), continuing to read Current after MoveNext returned false, or a manual enumeration loop that reads Current unconditionally each iteration.","commonSituations":"Hand-rolled while-loop enumerations of clocks; using Current after a break without checking position; reusing a stale enumerator after the collection moved; calling Current in a finally/log block after enumeration finished.","solutions":["Only read Current after a MoveNext() that returned true (or inside foreach)","Re-acquire GetEnumerator() instead of reusing a spent enumerator","Replace manual enumerator code with foreach or LINQ (ToList, FirstOrDefault, etc.)","Check MoveNext's return value before every Current access"],"exampleFix":"// before\nvar e = clockCollection.GetEnumerator();\nvar c = e.Current; // InvalidOperationException\n// after\nvar e = clockCollection.GetEnumerator();\nif (e.MoveNext()) { var c = e.Current; }","handlingStrategy":"try-catch","validationCode":"var e = clockCollection.GetEnumerator();\nbool hasCurrent = e.MoveNext();\nif (hasCurrent)\n{\n    var current = e.Current;\n}","typeGuard":"bool TryCurrent(IEnumerator<Clock> e, out Clock current)\n{\n    current = null;\n    return e != null && e.MoveNext() && (current = e.Current) != null;\n}","tryCatchPattern":"try\n{\n    var current = enumerator.Current;\n}\ncatch (InvalidOperationException)\n{\n    // enumerator not positioned on a valid element; reposition or re-enumerate\n}","preventionTips":["Only access Current after a successful MoveNext","Use foreach instead of manual enumerator code","Never reuse an enumerator after it has been exhausted; get a new one"],"tags":["wpf","animation","enumerator","invalidoperation"],"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"}