This is due to the new overloads to string.Join
added in version 4 of the .NET framework:
PS> [Environment]::Version.ToString()
2.0.50727.5456
PS> [string]::Join.OverloadDefinitions
static string Join(string separator, string[] value)
static string Join(string separator, string[] value, int startIndex, int count)
PS> [Environment]::Version.ToString()
4.0.30319.269
PS> [string]::Join.OverloadDefinitions
static string Join(string separator, Params string[] value)
static string Join(string separator, Params System.Object[] values)
static string Join[T](string separator, System.Collections.Generic.IEnumerable[T] values)
static string Join(string separator, System.Collections.Generic.IEnumerable[string] values)
static string Join(string separator, string[] value, int startIndex, int count)
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.
PS> Trace-Command *Member* { [string]::Join( "-", $word.ToCharArray() ) } -PSHost
...
MemberResolution: Method argument conversion.
MemberResolution: Converting parameter "-" to "System.String".
MemberResolution: Method argument conversion.
MemberResolution: Converting parameter "System.Char[]" to "System.String[]".
MemberResolution: Calling Method: static string Join(string separator, string[] value)
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 to string.Join
, which then has ToString
called to produce the "System.Char[]"
output.
Method argument conversion.
Converting parameter "-" to "System.String".
Method argument conversion.
Converting parameter "System.Char[]" to "System.Object".
Method argument conversion.
Converting parameter "System.Object[]" to "System.Object[]".
Calling Method: static string Join(string separator, Params System.Object[] values)
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 -join
operator, which works correctly everywhere :).
PS> $word = 'test'
PS> [string]::Join( '-', $word.ToCharArray() )
System.Char[]
PS> [string]::Join( '-', [string[]]$word.ToCharArray() )
t-e-s-t
PS> [char[]]$word -join '-'
t-e-s-t