arrays - 为什么@($null) 是$false,而@($null, $null) 是$true?

标签 arrays powershell

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

@() -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/

相关文章:

python - 如何仅增加 Python 列表中第一行的第一个元素?

php - 更改二维数组每一行中的键而不丢失值

java - 从数组中删除维度

powershell - 参数属性会以某种方式更改powershell 脚本输出吗?

powershell - 递归函数行为不正确

c# - 如何将数组拆分为一组 n 个元素?

c# - 从数组创建选择列表

.net - 在 64 位环境中使用 32 位 COM 对象

PowerShell 串联

powershell - 使用powershell读取Csv文件并捕获相应数据