Skip to content

Rule proposal: warn when using constructor for hashtables #576

@Jaykul

Description

@Jaykul

Creating a hashtable via the constructor without specifying an IEqualityComparer results in using the default IEqualityComparer (which uses Object::Equals), which means that strings are compared in a case-sensitive way. This may be confusing and unexpected to PowerShell scripters who are accustomed to case-insensitive hashtables.

Basically, I think you should warn people if you see:

New-Object System.Collections.Hashtable
[hashtable]::new()

Or any variation on those: (System.Collections.Hashtable = Collections.Hashtable = Hashtable) with or without parameters (unless the parameters include a custom IEqualityComparer).

The warning should recommend creating hashtables with the literal initializer @{} for speed (over New-Object) and case-sensitivity reasons as cited above.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions