| description | Avoid using ArrayList |
|---|---|
| ms.date | 04/16/2025 |
| ms.topic | reference |
| title | AvoidUsingArrayList |
Severity Level: Warning
Per .NET best practices, the ArrayList class is not recommended for new development,
the same recommendation applies to PowerShell:
Avoid the ArrayList class for new development.
The ArrayList class is a non-generic collection that can hold objects of any type.
This is in line 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
unintentionally pollutes the PowerShell pipeline and therefore might cause unexpected issues.
In cases where only the Add method is used, you might just replace the ArrayList class
with a generic List[Object] class but you could also consider using the idiomatic PowerShell
pipeline syntax instead.
# 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 { $_ }-
Enable: bool (Default value is$false)Enable or disable the rule during ScriptAnalyzer invocation.