Pradhyumna Khandekar
Pradhyumna Khandekar

Reputation: 31

Comparison in empty array and null in powershell

Code:

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

Output:

E
NE 

How is this possible ?

Upvotes: 3

Views: 1965

Answers (1)

woxxom
woxxom

Reputation: 73526

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.

Upvotes: 6

Related Questions