Array.Add vs +=

2020-01-27 01:13发布

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条回答
小情绪 Triste *
2楼-- · 2020-01-27 02:02

When using the $array.Add()-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.

$array += $element creates a new array with the same elements as old one + the new item, and this new larger array replaces the old one in the $array-variable

You can use the += operator to add an element to an array. When you use it, Windows PowerShell actually creates a new array with the values of the original array and the added value. For example, to add an element with a value of 200 to the array in the $a variable, type:

    $a += 200

Source: about_Arrays

+= is an expensive operation, so when you need to add many items you should try to add them in as few operations as possible, ex:

$arr = 1..3    #Array
$arr += (4..5) #Combine with another array in a single write-operation

$arr.Count
5

If that's not possible, consider using a more efficient collection like List or ArrayList (see the other answer).

查看更多
够拽才男人
3楼-- · 2020-01-27 02:10

If you want a dynamically sized array, then you should make a list. Not only will you get the .Add() functionality, but as @frode-f explains, dynamic arrays are more memory efficient and a better practice anyway.

And it's so easy to use.

Instead of your array declaration, try this:

$outItems = New-Object System.Collections.Generic.List[System.Object]

Adding items is simple.

$outItems.Add(1)
$outItems.Add("hi")

And if you really want an array when you're done, there's a function for that too.

$outItems.ToArray()
查看更多
成全新的幸福
4楼-- · 2020-01-27 02:15

The most common idiom for creating an array without using the inefficient += is something like this, from the output of a loop:

$array = foreach($i in 1..10) { 
  $i
}
$array
查看更多
登录 后发表回答