Skip to content

Make OrderedDictionaryKeyValueCollection implement IList #56835

@iRon7

Description

@iRon7

#15843 Ordered Dictionary keys are not numerically indexable

Just to formalize a .Net request originated from a PowerShell issue (The impact is low and I do have several workarounds):

Steps to reproduce

Indexing keys in a hash table does make much sense as the keys are unordered by design.
But are expected to work on an [ordered] dictionary.

$a = [ordered]@{ a = 1; b = 2; c = 3 }
$a.Keys[0] # $a.get_Keys()[0]
$a.Keys[1] # $a.get_Keys()[1]

Expected behavior

# Index 0 return a
$a.Keys[0] # $a.get_Keys()[0]
a

# Index 1 return b
$a.Keys[1] # $a.get_Keys()[1]
b

Actual behavior

# Index 0 returns all keys
$a.Keys[0] # $a.get_Keys()[0]
a
b
c

# Index 1 and successive indices return $Null
$a.Keys[1] # $a.get_Keys()[1]

Error details

No errors (unless strict mode is set)

Workarounds

@($a.Keys)[0]

([object[]]$dict.Keys)[0]

$dict.Keys.ForEach{ $_ }[0]

$keys = [object[]]::new($dict.Keys.Count)
$dict.Keys.CopyTo($keys, 0)
$keys[0]

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Suggested solution

Change the return type of IDictionary.Keys to IList

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions