Difference between $null and ""

This topic contains 7 replies, has 5 voices, and was last updated by Profile photo of TJ Anon TJ Anon 2 years, 1 month ago.

  • Author
    Posts
  • #20185
    Profile photo of TJ Anon
    TJ Anon
    Participant

    I'm trying to test a variable to see whether it has data in it. Looks like there is a difference between a set of quotes with nothing between them, and the variable, $null. This code tells me that my variable ($camp) is not equal to null, even though I assigned $null to it before testing.

       
       [string]$camp = $null
    
       if ($camp -eq $null)
          {write-host "camp equals null"}
       else
          {write-host "camp does not equal null"}
    

    The code below tells me that my variable is equal to a set of empty quotes.

       [string]$camp = $null
    
       if ($camp -eq "")
          {write-host "camp equals empty quotes"}
       else
          {write-host "camp does not equal empty quotes"}
    

    What's going on? What is $null? Why is it not the same as a set of empty quotes? I tried running get-member on $null and it wouldn't tell me anything, so I'm a little stumped.

  • #20186
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Correct. Empty string is not the same as null; you'd need to test specifically for that. Null is the non-existence of an object,
    Whereas a string is an object even if it's empty.

  • #20187
    Profile photo of TJ Anon
    TJ Anon
    Participant

    so I guess this statement is totally worthless...
    [string]$camp = $null

    I'm guessing that once a variable is a string, it can't be null? That's what my conditions are telling me. Right?

    The following codes results in "camp does not equal null"

       [string]$camp = $null
    
       if ($camp -eq $null)
          {write-host "camp equals null"}
       else
          {write-host "camp does not equal null"}
    
  • #20188
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's a common gotcha in PowerShell. For whatever reason, it won't let you assign a $null value to a string variable (or a string parameter to a .NET type); it gets converted to an empty string. This can cause some headaches when calling .NET methods that treat null and empty strings differently, which is why they later added (in v3, if I remember correctly), the [System.Management.Automation.NullString] class. If you want to call such a method, you do this:

    [SomeClass]::SomeMethod([nullstring]::Value)
    
  • #20203
    Profile photo of Simon Wåhlin
    Simon Wåhlin
    Participant

    To understand what happens in PowerShell I like to think about it like this:

    This code:

    [string]$camp = $null
    

    Will assign $Null to $Camp, but since the [string]-part forces $camp to be of the type string, $Camp will be assigned the value of [String]$Null

    [String]$Null will force a conversion of $Null (which is basically a non-existing object) to a string and in PowerShell that results in an empty string.

  • #20204
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    As far as PowerShell is concerned, that's basically correct. However, in the .NET Framework, strings really can be null, and there's a difference between a null string reference and an empty string. Sometimes that causes confusion when you're looking at .NET documentation and wondering why it doesn't seem to work properly from PowerShell.

  • #20211
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I've used [string]::IsNullOrEmpty($variable) to cover the bases when trying to see if a variable has an actual value

  • #20227
    Profile photo of TJ Anon
    TJ Anon
    Participant

    Poor Dave is starting to remember my M.O. I write a lot of code in GUIs using .Net. 😀 Thanks for the tips guys. I very much appreciate it!

You must be logged in to reply to this topic.