2

Why, in this powershell code, does the last statement resolve to true?

PS C:\> $s = [System.String]$null
PS C:\> $s.GetType()
IsPublic IsSerial Name                                     BaseType                                                                  
-------- -------- ----                                     --------                                                                  
True     True     String                                   System.Object 
PS C:\> $s -eq ""
True
3

2 Answers 2

1

PowerShell always converts null strings to empty string Check accepted answer here for details : Passing null to a mandatory parameter to a function

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

1 Comment

Sure, but the details there don't go into the question of why. It would be nice to understand the motivation here.
1

Basically so you never have to write string.IsNullOrEmpty() or get a null reference on a string in PowerShell. See Tony Hoare "Null References: The Billion Dollar Mistake".

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.