File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11---
22description : Avoid using ArrayList
3- ms.date : 08/31 /2025
3+ ms.date : 04/16 /2025
44ms.topic : reference
55title : AvoidUsingArrayList
66---
@@ -10,19 +10,24 @@ title: AvoidUsingArrayList
1010
1111## Description
1212
13- Important
13+ Per dotnet best practices, the
14+ [ ` ArrayList ` class] ( https://learn.microsoft.com/dotnet/api/system.collections.arraylist )
15+ is not recommended for new development, the same recommendation applies to PowerShell:
1416
1517Avoid the ArrayList class for new development.
1618The ` ArrayList ` class is a non-generic collection that can hold objects of any type. This is inline with the fact
1719that PowerShell is a weakly typed language. However, the ` ArrayList ` class does not provide any explicit type
1820safety and performance benefits of generic collections. Instead of using an ` ArrayList ` , consider using either a
1921[ ` System.Collections.Generic.List[Object] ` ] ( https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1 )
20- class or a fixed PowerShell array. Besides, the ` ArrayList.Add ` method returns the index of the added element which
21- often unintendedly pollutes the PowerShell pipeline and therefore might cause unexpected issues.
22-
22+ class or a fixed PowerShell array.
23+ Besides, the ` ArrayList.Add ` method returns the index of the added element which often unintendedly pollutes the
24+ PowerShell pipeline and therefore might cause unexpected issues.
2325
2426## How to Fix
2527
28+ In cases where only the ` Add ` method is used, you might just replace the ` ArrayList ` class with a generic
29+ ` List[Object] ` class but you could also consider using the idiomatic PowerShell pipeline syntax instead.
30+
2631## Example
2732
2833### Wrong
You can’t perform that action at this time.
0 commit comments