Starting from this Question I discovered that using the powershell.exe.config to load .net Framework 4.0 runtime, this code:
$word = "Thisisatest"
[System.String]::Join("-", $word.ToCharArray())
return System.Char[]
Executing Powershell w/o powershell.exe.config the same code works in the right way.
Someone can reproduce this issue and explain why??
My box is windows 7 pro with Powershell 2.0
Edit: my powershell.exe.config
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
This is due to the new overloads to
string.Joinadded in version 4 of the .NET framework:In .NET 2.0, the only applicable method overload takes a
string[]as the second argument, so each character from$word.ToCharArray()is coerced to a string.In .NET 4, the
object[]overload is preferred. Rather than boxing each character, the entire array is coerced to an object, sending a single-element object array tostring.Join, which then hasToStringcalled to produce the"System.Char[]"output.To get the old behavior, you can insert a
[string[]]cast to influence how the method is resolved (or in this case just use PowerShell’s-joinoperator, which works correctly everywhere :).