为什么@($空)为$假的,但@($空,空$)是$ true? [英] Why @($null) is $false, but @($null, $null) is $true?
问题描述
好奇,什么是这背后的理念是:
Curious, what is the idea behind this:
@() -as [bool]
# False
@($null) -as [bool]
# False
@($null, $null) -as [bool]
# True
我希望无论是假/真/真或假/假/假,但不是假/假/真。
I would expect either False/True/True or False/False/False, but not False/False/True.
推荐答案
作为PowerShell的快速reference¹概述:
As outlined in the Powershell Quick reference¹:
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
我觉得这背后的想法是,只有一个元素的数组 $假
如果你把一个小命令/函数的输出到一个数组可以很容易地发生。你可能希望有更多的价值,但有些错误使它返回 $假
,但 $假
数组两次可能是不太可能在这种情况下
I think the idea behind it was that an array with just one element $false
can easily happen if you put the output of a cmdlet/function into an array. You might expect more values but some error made it to return $false
, but $false
twice in the array might be unlikely in that case.
也许我们应该等待杰弗里Snover回答的位置: - )
Or maybe we should wait for Jeffrey Snover to answer here :-)
¹C:\\ WINDOWS \\ SYSTEM32 \\ WindowsPowerShell \\ V1.0 \\文档\\ EN-US \\ QuadFold.rtf我的机器上
¹ C:\Windows\System32\WindowsPowerShell\v1.0\Documents\en-US\QuadFold.rtf on my machine
这篇关于为什么@($空)为$假的,但@($空,空$)是$ true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!