is StringComparison.Ordinal the same as InvariantCulture for testing equality?

前端 未结 3 1352
暗喜
暗喜 2020-12-30 01:42

From their brief summary descriptions, it sounds like the string comparison rules StringComparison.Ordinal and StringComparison.InvariantCulture ar

相关标签:
3条回答
  • 2020-12-30 02:21

    It does matter, for example - there is a thing called character expansion

        var s1 = "Strasse";
        var s2 = "Straße";
    
        s1.Equals(s2, StringComparison.Ordinal);          // false
        s1.Equals(s2, StringComparison.InvariantCulture); // true
    

    With InvariantCulture the ß character gets expanded to ss.

    0 讨论(0)
  • 2020-12-30 02:26

    Well, it certainly matters. When you use an "ignore case" equality comparison then you're invoking a fairly massive chunk of code in the .NET framework that's aware of how casing rules work in the current culture. The rules of which are very interesting to a former-postage-stamp collector geek like me, there are some pretty odd-ball rules depending where you look. The Turkish I problem is famous, the Unicode dudes had to make an explicit exception for them.

    It isn't actually code btw, it's lookup tables. Interesting in itself because it requires MSFT to maintain the /linkres command line option for the C# compiler. A compile option you cannot use in your own projects. It's solely there to get mscorlib to be able to find the .nlp files, the conversion tables for culture rules. Stored in the same subdirectory of the GAC as mscorlib.dll, the effect of the compile option.

    But I digress. It stands to reason that StringComparison.OrdinalIgnoreCase is a wee bit quicker than StringComparison.InvariantCultureIgnoreCase. Just because 'invariant' means USA, home of MSFT. Hard to measure, this clocks in at nanoseconds. StringComparison.CurrentCultureIgnoreCase hits those translation tables. Dead slow when you first use it, just slower when you use them later.

    0 讨论(0)
  • 2020-12-30 02:28

    For the extra credit question

    • Comparison confusion: INVARIANT vs. ORDINAL

      ... the notion of an Ordinal sort was added and an Ordinal member was added to the CompareOptions enumeration. Selecting it would ignore all of those cultural collation features and give you a binary sort that would also, incidentally, not vary.

    • string comparison InvariantCultureIgnoreCase vs OrdinalIgnoreCase?

    • C#: String comparison guidelines and common usage

      The recommendation states that for culture-agnostic comparisons use the Ordinal and OrdinalIgnoreCase comparisons. These are fast and also safe. They rely on byte matching and are excellent options for matching strings for internal (non-UI) processing.

    0 讨论(0)
提交回复
热议问题