Could string comparisons really differ based on culture when the string is guaranteed not to change?

后端 未结 3 2090
醉酒成梦
醉酒成梦 2020-12-13 17:02

I\'m reading encrypted credentials/connection strings from a config file. Resharper tells me, \"String.IndexOf(string) is culture-specific here\" on this line:



        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 17:33

    CA1309: UseOrdinalStringComparison

    It doesn't hurt to not use it, but "by explicitly setting the parameter to either the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase, your code often gains speed, increases correctness, and becomes more reliable.".


    What exactly is Ordinal, and why does it matter to your case?

    An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast but culture-insensitive. When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.

    And, as you stated... the string value you are reading in is not culture sensitive, so it makes sense to use an Ordinal comparison as opposed to a Word comparison. Just remember, Ordinal means "this isn't culture sensitive".

提交回复
热议问题