为什么整数哈希键不是整数?该键的类型为 KeyCollection,即使它是从常量整数创建的。
产生此输出的脚本如下。我将仅显示最后三 (3) 行命令行。为什么键不是某种类型的整数?
PS C:\> $h.Keys[0].GetType() # the type of the first and only item in the set of keys is KeyCollection, not an int of some kind
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
False False KeyCollection System.Object
PS C:\> 1 -eq $h.Keys[0] # comparing 1 and the key both ways produces different results ???
False
PS C:\> $h.Keys[0] -eq 1
1
PS C:\> $PSVersionTable.PSVersion.ToString()
7.3.6
这是导致此情况的脚本。
$h = @{}
$hi1 = @{1='a';2='b'} # create a hash
$h.GetType() # yes, it is a hashtable
$h.Count # there should be none (0) at this point
$h[1] += @($hi1) # put an array containing a hash into h
$h[1] # check to see that the array containing a hash is in the hash h
$h[1].Count # there should be one (1) item in the hash
$h[1].GetType() # the item at hash index 1 should be an array
$h[1][0] # this is the first and only element in the array
$h[1][0].GetType() # the first item in the array is a hashtable
$h.Keys # there is only one key in the hash
$h.Keys.GetType() # the type of the Keys object is a KeyCollection
$h.Keys[0] # the first and only item in the set of keys is 1
$h.Keys[0].GetType() # the type of the first and only item in the set of keys is KeyCollection, not an int of some kind
1 -eq $h.Keys[0] # comparing 1 and the key both ways produces different results ???
$h.Keys[0] -eq 1