What's going on when you say: [string] $var

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 6 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #36440
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    The question is simple, but I'm not sure if the answer is. What happens on the second line here?

    $var = 1.5
    [string] $var

    One could think that the variable is cast. But the System.Double and System.String isn't based on each other so that wouldn't work in this case.

    Or one could think that the ToString method of System.Double is called, but this is not the case. If you're using a language that uses period as it's decimal separator you might not have noticed this. I'm norwegian and therefore use the nb-NO culture. With this culture the following:

    $var = 1.5
    [string] $var
    $var.ToString()

    Will output:
    1.5
    1,5
    (This could give you an headache since you'll get 15 if you run "[int] $var.ToString()")

    So what happens? In which cases will "[someclass] $somevar" work and what is actually happening?

    #36441
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It is converting the value to a string, but PowerShell's type conversion logic goes way beyond simple casting that would be available in a C# or other .NET app. Check out this blog post from the PowerShell team for more details on all the work that it does for you:

    https://blogs.msdn.microsoft.com/powershell/2013/06/11/understanding-powershells-type-conversion-magic/

    #36487
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

    Thanks @dlwyatt

    That post is actually quite helpful. Do you know if it's still up to date? This isn't documented on technet?

    #36490
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm not sure if it's still up to date. However, I poked around a bit, and it looks like this logic is just about completely found in the class [System.Management.Automation.LanguagePrimitives] , in the System.Management.Automation assembly. ([string]$var) seems to be the equivalent if ([System.Management.Automation.LanguagePrimitives]::ConvertTo($var, [string])), and if you're really interested in seeing under the hood, you can take a look at the implementation by opening that assembly in something like ILSpy or dotPeek. 🙂

    #36491
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Also, specifically for numbers, I found that this is what's happening for numeric to string conversions:

    result = (string)Convert.ChangeType(valueToConvert, resultType, CultureInfo.InvariantCulture.NumberFormat);
    

    So that's why you're seeing a difference in output between ([string]$var) and ($var.ToString()) ; with the [string] "cast", PowerShell is hard-coded to use InvariantCulture instead of CurrentCulture.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.