DE0006: Non-generic collections shouldn't be used (especially in Pester tests)
See original GitHub issueAs 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 typeArray
; in most cases the performance can be neglected using+=
operator on@()
on very small arrays; some instances might be refactored to use aForEach
loop which returns anArray
)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Individual rules are adjusted by configuration files in the repo. See the services web site for instructions, on how to add a configuration.
Codacy has been disable for C# because it is using an old version of C# for analysis
Also to be clear, DE0006 recommends avoiding the use of non-generic collections in new code. For existing code we can consider the benefits gained from converting to generic collections where possible (i.e. for PowerShell, where such changes would be non-breaking), but that’s it.