为什么@($ null)是$ false,但@($ null,$ null)是$ true?

ale*_*2k8 3 arrays powershell

好奇,这背后的想法是什么:

@() -as [bool]
# False

@($null) -as [bool] 
# False

@($null, $null) -as [bool]
# True
Run Code Online (Sandbox Code Playgroud)

我希望False/True/True或False/False/False,但不是False/False/True.

Joe*_*oey 7

正如Powershell快速参考文献中所述:

True                                                 False 
~~~~                                                 ~~~~~
$TRUE                                                $FALSE 
Any string of length > 0, except the word “false”    Empty string or the string “false” 
Any number ? 0                                       Any number = 0 
Array of length > 1                                  Array of length 0 
Array of length 1 whose element is true              Array of length 1 whose element is false 
A reference to any object                            Null 

我认为它背后的想法是,$false如果将cmdlet/function的输出放入数组中,则只有一个元素的数组很容易发生.您可能期望更多的值但是有一些错误使它返回$false,但$false在这种情况下,数组中的两次可能不太可能.

或许我们应该等待Jeffrey Snover在这里回答:-)

我机器上的C:\ Windows\System32\WindowsPowerShell\v1.0\Documents\en-US\QuadFold.rtf