Skip to content

StringComparer is not retained when returning HashSet from function #24370

@shekarreddyb

Description

@shekarreddyb

Prerequisites

Steps to reproduce

Steps to reproduce:

Script

function Get-HashSet {
    $hashSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
    $hashSet.Add("ABCD1")
    $hashSet.Add("ABCD2")
    return $hashSet
}
# Get the HashSet reference from the function
$myHashSet = Get-HashSet
# Check if the same reference is maintained and comparer works
$myHashSet.Contains("abcd1")  # Should return True, But Retuning False

# do same with hasset created outside of function
$hashSetInline = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$hashSetInline.Add("ABCD1") | Out-Null
$hashSetInline.Add("ABCD2") | Out-Null
$hashSetInline.Contains("abcd1")  #Returns True as expected

$PSVersionTable

Expected behavior

True
True

Actual behavior

False
True

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0-preview.5
PSEdition                      Core
GitCommitId                    7.5.0-preview.5
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

and on all old versions

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.Resolution-AnsweredThe question is answered.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions