.NET 5.0 - Non-empty string is equal to empty string
I am encountering unexpected behavior either in the latest release of powershell or .net; I'm not sure which.
Quick demonstration of unexpected behavior:
PS > "$($PSVersionTable.PSVersion)"; "`u{00001A}" -eq ""; `
-not [String]::Compare("", "`u{00001A}"); `
-not [String]::Compare("", "`u{00001A}", [CultureInfo]::CurrentCulture, "None"); `
-not [String]::Compare("", "`u{00001A}", [CultureInfo]::CurrentCulture, "Ordinal");
7.1.0
True
True
True
False
Whereas in an older version of PowerShell:
5.1.18362.1171
False
False
False
False
I tried reproducing the problem under different versions of .NET and powershell, shown below. The best I can tell according to System.Globalization.CompareOptions, is that "the default option settings for string comparisons [when CompareOptions.None is used]" changed. However, I can't find any documentation on what "the default option settings" are.
It was recommended I cross-post this issue from: https://github.com/PowerShell/PowerShell/issues/14127
Steps to reproduce:
In any version of PowerShell, run the code below. For .NET, compile just the embedded C# snippet.
# older powershell requires extra parameters
$runtime = [System.Runtime.InteropServices.RuntimeInformation, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]::FrameworkDescription
Add-Type -TypeDefinition @"
using System.Globalization;
public class CompareTest {
public static System.String run() {
System.String result = "";
result += "" == "\x1A";
result += "\n";
result += System.String.Compare("", "\x1A");
result += "\n";
result += System.String.Compare("", "\x1A", CultureInfo.CurrentCulture, CompareOptions.None);
result += "\n";
result += System.String.Compare("", "\x1A", CultureInfo.CurrentCulture, CompareOptions.Ordinal);
result += "\n";
return result;
}
}
"@ `
-ReferencedAssemblies System.Globalization `
-Language CSharp; `
`
@"
psversion = $($PSVersionTable.PSVersion)
Runtime = $runtime
====
$([CompareTest]::run())
"@
PowerShell 5 / .NET 4:
psversion = 5.1.18362.1171
Runtime = .NET Framework 4.8.4250.0
====
False
-1
-1
-26
PowerShell 7 / .NET 5:
psversion = 7.1.0
Runtime = .NET 5.0.0
====
False
0
0
-1
Tagging subscribers to this area: @tarekgh, @safern, @krwq See info in area-owners.md if you want to be subscribed.
Issue Details
I am encountering unexpected behavior either in the latest release of powershell or .net; I'm not sure which.
Quick demonstration of unexpected behavior:
PS > "$($PSVersionTable.PSVersion)"; "`u{00001A}" -eq ""; `
-not [String]::Compare("", "`u{00001A}"); `
-not [String]::Compare("", "`u{00001A}", [CultureInfo]::CurrentCulture, "None"); `
-not [String]::Compare("", "`u{00001A}", [CultureInfo]::CurrentCulture, "Ordinal");
7.1.0
True
True
True
False
Whereas in an older version of PowerShell:
5.1.18362.1171
False
False
False
False
I tried reproducing the problem under different versions of .NET and powershell, shown below. The best I can tell according to System.Globalization.CompareOptions, is that "the default option settings for string comparisons [when CompareOptions.None is used]" changed. However, I can't find any documentation on what "the default option settings" are.
It was recommended I cross-post this issue from: https://github.com/PowerShell/PowerShell/issues/14127
Steps to reproduce:
In any version of PowerShell, run the code below. For .NET, compile just the embedded C# snippet.
# older powershell requires extra parameters
$runtime = [System.Runtime.InteropServices.RuntimeInformation, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]::FrameworkDescription
Add-Type -TypeDefinition @"
using System.Globalization;
public class CompareTest {
public static System.String run() {
System.String result = "";
result += "" == "\x1A";
result += "\n";
result += System.String.Compare("", "\x1A");
result += "\n";
result += System.String.Compare("", "\x1A", CultureInfo.CurrentCulture, CompareOptions.None);
result += "\n";
result += System.String.Compare("", "\x1A", CultureInfo.CurrentCulture, CompareOptions.Ordinal);
result += "\n";
return result;
}
}
"@ `
-ReferencedAssemblies System.Globalization `
-Language CSharp; `
`
@"
psversion = $($PSVersionTable.PSVersion)
Runtime = $runtime
====
$([CompareTest]::run())
"@
PowerShell 5 / .NET 4:
psversion = 5.1.18362.1171
Runtime = .NET Framework 4.8.4250.0
====
False
-1
-1
-26
PowerShell 7 / .NET 5:
psversion = 7.1.0
Runtime = .NET 5.0.0
====
False
0
0
-1
| Author: | gurutech-thomasr |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Nevermind; closing. I found my answer by searching existing issues. This is an intended breaking change in .NET 5, according to:
Behavior changes when comparing strings on .NET 5+ .NET globalization and ICU