Skip to content

Instantly share code, notes, and snippets.

@aadennis
Created July 7, 2018 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadennis/b2f034246fbb7c4571a42afd45810f3c to your computer and use it in GitHub Desktop.
Save aadennis/b2f034246fbb7c4571a42afd45810f3c to your computer and use it in GitHub Desktop.
# snippet to illustrate problems with ArrayList and pipelines
function bar() {
$collectionA = [System.Collections.ArrayList] @()
$collectionA.Add("row 1")
$collectionA.Add("row 2")
$collectionA.Add("row 3")
$collectionA.Add("row 4")
$collectionB = [System.Collections.ArrayList] @()
<#
$currIndex = 0
$collectionA | % {
$curr = $_
$currIndex++
if (($currIndex % 2) -eq 0) {
$collectionB.Add($curr)
}
}
#>
$collectionB
}
[System.Collections.ArrayList] $foo = bar
# With the code as-is, you might expect bar() to return an empty collection (collectionB).
# No: it returns placeholders for collectionA, and anything it has for collectionB
$foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment