PowerShell / PowerShell/PowerShell
.GetHashCode() should not report the same value for all instances of [pscustomobject]
@JamesWTruher is already working on this.
Since Oct 27, 2021.
- Dominant language
- C#
- Stars
- 55.5k
- Forks
- 8.5k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 88
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.