Skip to content

Latest commit

 

History

History
67 lines (49 loc) · 1.69 KB

File metadata and controls

67 lines (49 loc) · 1.69 KB
description Avoid using ArrayList
ms.date 07/21/2026
ms.topic reference
title AvoidUsingArrayList

AvoidUsingArrayList

Severity Level: Warning

Default state: Disabled

Description

Avoid the ArrayList class for new development. The documentation for the ArrayList class recommends using the System.Collections.Generic.List<T> class instead.

The ArrayList class is a non-generic collection that can hold objects of any type. The generic List<T> class provides better performance, type safety, and the List<T>.Add() method doesn't have the output side-effects that ArrayList.Add() has.

In cases where only the ArrayList.Add() method is used, you could replace the ArrayList class with a generic List[Object] class or consider using the more idiomatic PowerShell pipeline syntax instead.

Example

Noncompliant

# 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

Compliant

# 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 { $_ }

Configure rule

Rules = @{
    PSAvoidUsingArrayList = @{
        Enable = $true
    }
}

Parameters

  • Enable: bool (Default value is $false)

    Enable or disable the rule during ScriptAnalyzer invocation.