在Powershell中测试null:为什么测试空数组的行为与空字符串不同?

alx*_*x9r 5 arrays powershell null types

考虑这段代码

"Type: array"
$v = @()
"  -eq `$null: $($v -eq $null)"
"  -ne `$null: $($v -ne $null)"

"Type: string"
$v = ''
"  -eq `$null: $($v -eq $null)"
"  -ne `$null: $($v -ne $null)"
Run Code Online (Sandbox Code Playgroud)

产生以下结果:

Type: array
  -eq $null: 
  -ne $null: 
Type: string
  -eq $null: False
  -ne $null: True
Run Code Online (Sandbox Code Playgroud)
  1. 为什么-eq-ne像预期的那样一个空字符串,但评价为空或空的空数组?

  2. 更重要的是,你如何区分$null和空数组?

mjo*_*nor 6

当比较运算符用于集合时,它们不返回布尔值($ true或$ false).它们返回满足条件的集合的所有成员.

看到:

Get-help About_Comparison_Operators