You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attempting modification of a collection while it is being enumerated should result in an error:
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.
This error isn't consistently surfaced - note that this is a regression from Windows PowerShell:
# OK: Reports an error for the attempt to modify the collection being enumerated.'--- foreach'$l= [System.Collections.Generic.List[string]] @('one','two')
foreach ($ein$l) { $l.Remove($e) }
# !! Fails QUIETLY in PS CORE as of v7.4.0-preview.5'--- pipeline'$l= [System.Collections.Generic.List[string]] @('one','two')
$l|ForEach-Object { $l.Remove($_) }
Important: The quiet failure only occurs when the code is executed via a script file; if you paste the last two statements into an interactive console, the error does surface.
[Update: NOT PowerShell's fault - see below] The following WinForms example affects Windows PowerShell too (both interactively and in a script):
usingnamespaceSystem.Windows.FormsAdd-Type-Assembly System.Windows.Forms
$f= [Form]::new()
$f.Controls.AddRange((
[CheckBox]::new(),
[CheckBox]::new()
))
# !! Should fail with an error message, but quietly aborts after the first .Remove() call.$f.Controls|ForEach-Object { $f.Controls.Remove($_) }
$f.Controls.Count# !! still nonzero
Expected behavior
First snippet:
Both the foreach and the ForEach-Object statement should report the expected error.
Second snippet:
The expected error should occur.
Actual behavior
First snippet:
The ForEach-Object statement doesn't report an error and quietly aborts the pipeline after the first .Remove() call
Second snippet:
The expected error doesn't occur; quietly aborts the pipeline after the first .Remove() call.
Prerequisites
Steps to reproduce
Attempting modification of a collection while it is being enumerated should result in an error:
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.This error isn't consistently surfaced - note that this is a regression from Windows PowerShell:
Important: The quiet failure only occurs when the code is executed via a script file; if you paste the last two statements into an interactive console, the error does surface.
[Update: NOT PowerShell's fault - see below] The following WinForms example affects Windows PowerShell too (both interactively and in a script):
Expected behavior
First snippet:
foreachand theForEach-Objectstatement should report the expected error.Second snippet:
Actual behavior
First snippet:
ForEach-Objectstatement doesn't report an error and quietly aborts the pipeline after the first.Remove()callSecond snippet:
.Remove()call.Error details
No response
Environment data
Visuals
No response