| description | Avoid reserved words as function names |
|---|---|
| ms.date | 08/31/2025 |
| ms.topic | reference |
| title | AvoidUsingArrayList |
Severity Level: Warning
Important
Avoid the ArrayList class for new development.
The ArrayList class is a non-generic collection that can hold objects of any type. This is inline with the fact
that PowerShell is a weakly typed language. However, the ArrayList class does not provide any explicit type
safety and performance benefits of generic collections. Instead of using an ArrayList, consider using either a
System.Collections.Generic.List[Object]
class or a fixed PowerShell array. Besides, the ArrayList.Add method returns the index of the added element which
often unintendedly pollutes the PowerShell pipeline and therefore might cause unexpected issues.
# Using an ArrayList
$List = [System.Collections.ArrayList]::new()
1..3 | ForEach-Object { $List.Add($_) } # Note that this will return the index of the added element# Using a generic List
$List = [System.Collections.Generic.List[Object]]::new()
1..3 | ForEach-Object { $List.Add($_) } # This will not return anything# Creating a fixed array by using the PowerShell pipeline
$List = 1..3 | ForEach-Object { $_ }