Created
July 7, 2018 22:35
-
-
Save aadennis/b2f034246fbb7c4571a42afd45810f3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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