很好奇,这背后的想法是什么:
@() -as [bool]
# False
@($null) -as [bool]
# False
@($null, $null) -as [bool]
# True
我希望是 False/True/True 或 False/False/False,但不是 False/False/True。
最佳答案
如 Powershell 快速引用 1 中所述:
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
我认为其背后的想法是,如果将 cmdlet/函数的输出放入数组中,则很容易出现只有一个元素 $false
的数组。您可能期望更多值,但某些错误使其返回 $false
,但在这种情况下,数组中出现两次 $false
的可能性不太可能。
或者也许我们应该等待 Jeffrey Snover 在这里回答:-)
¹ 我的计算机上的 C:\Windows\System32\WindowsPowerShell\v1.0\Documents\en-US\QuadFold.rtf
关于arrays - 为什么@($null) 是$false,而@($null, $null) 是$true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/785877/