increase

Why should I avoid using the increase assignment operator (+=) to create a collection

删除回忆录丶 提交于 2020-03-24 00:21:08
问题 The increase assignment operator ( += ) is often used in [PowerShell] questions and answers at the StackOverflow site to construct a collection objects, e.g.: $Collection = @() 1..$Size | ForEach-Object { $Collection += [PSCustomObject]@{Index = $_; Name = "Name$_"} } Yet it appears an very inefficient operation. Is it Ok to generally state that the increase assignment operator ( += ) should be avoided for building an object collection in PowerShell? 回答1: Yes, the increase assignment operator