Testing collection for null

Why, when I test the collection for nulland set the collection as the first parameter, it returns the collection if it is not null, and if it is null, it returns null.

I realized that if I drop it on [bool], it will fix the problem. But I thought that -eqdisplays the value Boolwithout the need for casting?

$objectArray = @('a','b','c','d')

$objectArray -ne $null
<#
a
b
c
d
#>

[bool]($objectArray -ne $null)
#True

$objectArray -eq $null
#Nothing is outputted

[bool]($objectArray -eq $null)
#False

$null -ne $objectArray
#True

$null -eq $objectArray
#False

When I check the normal string, I get the expected results, as shown below:

$object = 'a'

$object -ne $null
#True

$object -eq $null
#False

$null -ne $object
#True

$null -eq $object
#False
+4
source share
2 answers

" , ", , ". , , ( , t). , , .

'a','b','c' -eq 'b'       #output is 'b'

1,2,3,4,5 -lt 3           #output is 1,2

1,2,$null,4,5 -ne $null   # output is 1,2,4,5

1,2,$null,4,5 -eq $null   # output is $null

:

[bool]($objectArray -ne $null)
#True
#filters the array, returns some values
#arrays with some things in them cast to true

$objectArray -eq $null
#Nothing is outputted
# nothing in the array was null, no output.

[bool]($objectArray -eq $null)
#False
# filters the array, empty arrays cast to false

,     return Boolean value. ,      .      .

(-Contains, -NotContains),      In (-In, -NotIn) (-Is, -IsNot),      .

+3

TessellatingHeckler:

, LHS , , , [bool], , - LHS:

# ALWAYS returns a [bool], because the LHS (left-hand side) is a SCALAR:
$null -eq $someVariableThatMayBeAscalarOrAnArray

, , Boolean,, , .
() LHS , .

0

Source: https://habr.com/ru/post/1659677/


All Articles