Array.Add vs +=

I’ve found some interesting behaviour in PowerShell Arrays, namely, if I declare an array as:

$array = @()

And then try to add items to it using the $array.Add("item") method, I receive the following error:

Exception calling “Add” with “1” argument(s): “Collection was of a fixed size.”

However, if I append items using $array += "item", the item is accepted without a problem and the “fixed size” restriction doesn’t seem to apply.

Why is this?

3 Answers
3

Leave a Comment