You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In what conditions does operator [] return AutomationNull? #6436
Chapter 7.1.4 of the PowerShell spec indicates that for arrays, strings, hashtables, and XML documents operator [] returns $null for non-existent indexes. The spec seems to be silent for other types, however. I expected that, for other types, operator [] would also return $null for non-existent indexes. It seems that for some combinations of types and indexes, however, operator [] returns AutomationNull. In what conditions does operator [] return AutomationNull?
Steps to reproduce
&{(1)[1]} |% {'output from indexing [int] out-of bounds'}
&{(,1)[1]} |% {'output from indexing array out-of-bounds'}
Expected behavior
output from indexing [int] out-of bounds
output from indexing array out-of-bounds
Actual behavior
output from indexing array out-of-bounds
Environment data
>$PSVersionTable
Name Value
---------
PSVersion 6.0.0
PSEdition Core
GitCommitId v6.0.0
OS Microsoft Windows 6.3.9600
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
It looks like this is different behavior from selecting a non-existent property. Selecting a non-existent property seems to always output $null not AutomationNull:
&{(1).NonExistent} |% {'output from selecting non-existent property on [int]'}
&{(,1).NonExistent} |% {'output from selecting non-existent property on [array]'}
outputs
output from selecting non-existent property on [int]
output from selecting non-existent property on [array]
Activity
alx9r commentedon Mar 20, 2018
I think the logic is here, but I only have a guess at what that code does.
alx9r commentedon Mar 21, 2018
It looks like this is different behavior from selecting a non-existent property. Selecting a non-existent property seems to always output
$nullnotAutomationNull:outputs
mklement0 commentedon Mar 22, 2018
Indeed, accessing a nonexistent property always returns
$null, whether applied to a scalar or a collection.By contrast, indeed,
[System.Management.Automation.Internal.AutomationNull]::Value,$nullwhich is an interesting inconsistency (for which I have no explanation).