Skip to content

Rule request: AvoidArrayList #2147

Closed

Description

@iRon7

Aside from the important remark:

Important

We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List class. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:

For a heterogeneous collection of objects, use the List<Object> (in C#) or List(Of Object) (in Visual Basic) type.
For a homogeneous collection of objects, use the List<T> class. See Performance Considerations in the List reference topic for a discussion of the relative performance of these classes. See Non-generic collections shouldn't be used on GitHub for general information on the use of generic instead of non-generic collection types.

The ArrayList.Add method returns an index of the added element which often unintendedly pollutes the PowerShell pipeline and causes unexpected issues, see e.g.:

Note that an ArrayList is not typesafe which is consistent with PowerShell as a loosely language.
Alternatives for a ArrayList type are:

  • [object[]] which is also loosely but not sizable, or
  • [System.Collections.Generic.List[Object]] with is both sizable and loosely.

Activity

  1. bergmeister commented on Mar 31, 2026

    @bergmeister
    Collaborator

    Could be added although not PowerShell specific. What would be great is to run C# analyzers in PSScriptAnalyzer because they would cover much more and things like that likely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions