JochCool
JochCool
CC#
Created by JochCool on 2/2/2024 in #help
Getting warning CA1309 when explicitly setting string comparison
I have code analysis mode set to Recommended in my project because it often helps me find subtle bugs. It triggered a warning (CA1309: Use ordinal string comparison) on this line of code, but I don't know why:
C#
if (string.Equals(otherName, name, StringComparison.InvariantCultureIgnoreCase))
C#
if (string.Equals(otherName, name, StringComparison.InvariantCultureIgnoreCase))
If I look at the docs page for this rule, it seems to me like this was intended for situations like name.CompareTo(otherName) where you might accidentally use the thread's current culture, which is bad. But in this case I am explicitly saying that I want a linguistic sort, not an ordinal sort, because names that are basically the same should be treated as the same. So is this warning wrong? Or am I misunderstanding how the method works? Or are there downsides to using the invariant culture?
9 replies