M-JM
M-JM

Reputation: 103

Why does String.Equals with InvariantCultureIgnoreCase behave different in .net 4.7.2 and .net 5?

I have the following line of code:

String.Equals("strasse", "straße", StringComparison.InvariantCultureIgnoreCase)

In .net 4.7.2, this returns true.
In .net 5 (and .net 6), this returns false.

Why?


I'm currently learning how comparing strings works in C#. NET. and have come across an unexpected result that I do not fully understand.

When using the overloaded method String.Equals(string,string,Stringcomparison) to compare string :"strasse" and string : "straße" with the following Stringcomparison :

Console.WriteLine(String.Equals("strasse", "straße", StringComparison.OrdinalIgnoreCase));
Console.WriteLine(String.Equals("strasse", "straße", StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine(String.Equals("strasse", "straße", StringComparison.InvariantCultureIgnoreCase));

I get the following result :

False 
False
False

I expected the first one to return false but both the second and third line to return true. I first though maybe my CurrentCulture was the issue, so to be sure is et both the CurrentCulture and CurrentUICulture to :

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("de-DE");

Did I incorrectly understand String comparison ? or am I missing something obvious here ?

Thanks in advance for anyone willing to help me understand

Upvotes: 5

Views: 943

Answers (1)

Yennefer
Yennefer

Reputation: 6234

When you target .NET Framework 4.x you are implicitly targeting a Windows Platform. Windows platforms handle Unicode and Cultures in their specific way, which is the direct byproduct of the evolution of the platform during the last 30 years. The standard for Windows platforms is using NLS Apis.

However, this is not the case for other platforms.

During the past years the ICU project tried to provide a unified, de-facto standard for handling Unicode characters (and more). Since .NET Core runs by design on Windows, Linux, Mac and (possibly) Android devices, you expect your application to behave consistently regardless the platform it runs on

For this reason, .Net Core 5 switched to ICU libraries as a breaking change. You can find more information on Globalization APIs use ICU libraries on Windows. ICU libraries are available for many languages and are interoperable, thus allowing better integration.

Upvotes: 3

Related Questions