3

Code:

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

Output:

E
NE 

How is this possible ?

1
  • The above scenario changes when you use -eq instead of -ne! This is indeed very curious. Please include this when considering your answers. Result: "E", "E" Commented Aug 24, 2018 at 8:06

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.