dotnet/wpf · error · InvalidOperationException
SR.Enumerator_NotStarted
Error message
SR.Enumerator_NotStarted
What it means
The VisualCollection Enumerator's Current property throws InvalidOperationException(SR.Enumerator_NotStarted) when MoveNext has never been called before accessing Current. The enumerator's _index is still -1, so there is no current element to return.
Solutions
- Call MoveNext() at least once and check its return value before reading Current
- Prefer foreach over manual enumerator usage, which enforces this ordering automatically
- Restructure manual loops to follow the standard while (enumerator.MoveNext()) { use Current } pattern
Example fix
// before
var e = visualCollection.GetEnumerator();
var first = e.Current; // throws: not started
// after
var e = visualCollection.GetEnumerator();
if (e.MoveNext()) { var first = e.Current; } Defensive patterns
Strategy: try-catch
Validate before calling
// no pre-call validation possible; use foreach or check via enumerator state
Try / catch
try { var cur = e.Current; }
catch (InvalidOperationException) { /* MoveNext not yet called; call it first */ } Prevention
- Prefer foreach, which never reads Current before MoveNext
- In manual loops, always branch on MoveNext()'s return before Current
- Do not read Current outside the iteration body
When it happens
Trigger: Creating an enumerator via VisualCollection.GetEnumerator() and reading Current (or the foreach iteration variable pattern manually) before the first MoveNext() call.
Common situations: Hand-written enumerator loops that read Current in an initialization step; custom code that stores enumerator.Current outside a foreach; foreach is safe — only manual enumerator use hits this.
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/b13fc7248c5a20e9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs:947
get
{
return this.Current;
}
}
/// <summary>
/// Gets the current Visual.
/// </summary>
public Visual Current
{
get
{
if (_index < 0)
{
if (_index == -1)
{
// Not started.
throw new InvalidOperationException(SR.Enumerator_NotStarted);
}
else
{
// Reached the end.
Debug.Assert(_index == -2);
throw new InvalidOperationException(SR.Enumerator_ReachedEnd);
}
}
return _currentElement;
}
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
public void Reset()View on GitHub (pinned to 81131a70a4)