dotnet/wpf · error · InvalidOperationException
SR.LocalValueEnumerationOutOfBounds
Error message
SR.LocalValueEnumerationOutOfBounds
What it means
LocalValueEnumerator.Current throws InvalidOperationException(SR.LocalValueEnumerationOutOfBounds) when the enumerator has moved past the last element (MoveNext returned false and iteration ended). This implements the documented IEnumerator.Current behavior. Reading Current after enumeration has completed is a caller sequencing error.
Solutions
- Only access Current when MoveNext() returns true.
- Replace manual enumerator code with a foreach loop.
- Reset the enumerator via Reset() if you need to iterate again (and then MoveNext first).
- Capture values inside the loop instead of dereferencing Current afterwards.
Example fix
// before
var e = obj.GetLocalValueEnumerator();
while (e.MoveNext()) { /* ... */ }
var last = e.Current; // throws: past end
// after
var e = obj.GetLocalValueEnumerator();
LocalValueEntry last = null;
while (e.MoveNext()) { last = e.Current; } Defensive patterns
Strategy: validation
Validate before calling
// read Current only inside: while (e.MoveNext()) { use e.Current; } Type guard
null
Try / catch
try { var cur = e.Current; }
catch (InvalidOperationException) { /* enumeration already finished */ } Prevention
- Check MoveNext()'s return value before every Current access.
- Use foreach to eliminate manual sequencing bugs.
- Do not reuse spent enumerators; create a new one via GetLocalValueEnumerator().
When it happens
Trigger: Continuing to read Current after MoveNext() has returned false; calling Current again after the loop finished; reusing a spent enumerator.
Common situations: do/while loops that read Current before checking MoveNext's return value; caching the enumerator and dereferencing Current later; custom collection processors that run one iteration too many.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- InvalidOperationException
- SR.Enumerator_CollectionChanged
- SR.Enumerator_NotStarted
- SR.Enumerator_NotStarted
- SR.Enumerator_NotStarted
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0325d6300cd881a4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/LocalValueEnumerator.cs:76
}
/// <summary>
/// Get current entry
/// </summary>
public LocalValueEntry Current
{
get
{
if(_index == -1 )
{
// IEnumerator.Current is documented to throw this exception
throw new InvalidOperationException(SR.LocalValueEnumerationReset);
}
if(_index >= Count )
{
// IEnumerator.Current is documented to throw this exception
throw new InvalidOperationException(SR.LocalValueEnumerationOutOfBounds);
}
return _snapshot[_index];
}
}
/// <summary>
/// Get current entry (object reference based)
/// </summary>
object IEnumerator.Current
{
get { return Current; }
}
/// <summary>
/// Move to the next item in the enumerator
/// </summary>
/// <returns>Success of the method</returns>View on GitHub (pinned to 81131a70a4)