Skip to content

.GetHashCode() should not report the same value for all instances of [pscustomobject] #15806

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

[pscustomobject] is a reference type without overridden equality-comparison behavior, so testing for equality performs reference equality, i.e. the test only succeeds if to values point to the very same instance.

Such types usually use the default (inherited) .GetHashCode() implementation, which for reference types means a value that is derived from the instance's identity.

Unexpectedly, all [pscustomobject] instances return the same from their .GetHashCode() method, which makes [pscustomobject] unsuitable as a hashtable key, for instance.

Expected behavior

PS> ([pscustomobject] @{ foo=1 }).GetHashCode() -eq ([pscustomobject] @{ foo=1 }).GetHashCode()
False # Two distinct [pscustomobject] instances that compare unequally should have distinct hash codes.

Actual behavior

The above returns $true, even though [pscustomobject] @{ foo=1 } -eq [pscustomobject] @{ foo=1 } returns $false (due to reference equality being tested).

Error details

No response

Environment data

PowerShell Core 7.2.0-preview.7

Visuals

No response

Metadata

Metadata

Assignees

Labels

KeepOpenThe bot will ignore these and not auto-closeResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions