Skip to content

DE0006: Non-generic collections shouldn't be used (especially in Pester tests) #10315

@ThreeFive-O

Description

@ThreeFive-O

As per .NET Platform compatibility rule DE0006 and PowerShell coding guidelines non-generic collections shouldn't be used.

In particular relevant for PowerShell would be System.Collections.ArrayList and System.Collections.Hashtable.

Most occurrences of ArrayList are in C# code obviously, but some can be found in Pester test files too (due to the lack of an internal dynamic array type in PowerShell).

Those two styles are found in Pester tests:

  • New-Object System.Collections.ArrayList
  • [System.Collections.ArrayList]::new()

C# code
The replacement of the non-generic collections in C# code by the generic ones might be non-trivial due to the type problems. For new code and code changes the rule should be followed as the replacement is an on-going process.

Pester tests
For Pester test files the PowerShell syntax should be favored over the explicitly typed C# equivalents:

  • System.Collections.Hashtable -> @{}
  • System.Collections.ArrayList -> @() (though being of type Array; in most cases the performance can be neglected using += operator on @() on very small arrays; some instances might be refactored to use a ForEach loop which returns an Array)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Code Cleanupthe issue is for cleaning up the code with no impact on functionalityResolution-FixedThe issue is fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions