Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison in empty array and null in powershell

Tags:

powershell

Code:

$arr=@() 
if($arr -ne $null){"NE"} else{"E"} 
if($null -ne $arr){"NE"} else{"E"} 

Output:

E
NE 

How is this possible ?

like image 674
Pradhyumna Khandekar Avatar asked Sep 17 '26 08:09

Pradhyumna Khandekar


1 Answers

The first if compares each element of the array to $null and produces a collection of non-null elements, which in your case is empty, thus it's false and else displays E.

The second if compares a single object $null with another object $arr and since $arr itself is not $null (as an object that stores an empty collection inside) it displays NE.

like image 61
wOxxOm Avatar answered Sep 21 '26 00:09

wOxxOm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!