CA1310:为了确保正确,请指定 StringComparisonCA1310: Specify StringComparison for correctness

规则 IDRule ID CA1310CA1310
类别Category 全球化Globalization
修复是中断或非中断Fix is breaking or non-breaking 不间断Non-breaking


字符串比较运算使用方法重载,该重载不设置 StringComparison 参数并默认使用特定于区域性的字符串比较。A string comparison operation uses a method overload that does not set a StringComparison parameter and uses culture-specific string comparison by default. 因此,它的行为会因当前用户的区域设置而异。Hence, its behavior will vary based on the current user's locale settings.

规则说明Rule description

默认情况下,默认情况下,使用特定于区域性的字符串比较的字符串比较方法可能会无意中意外运行时行为。A string comparison method that uses culture-specific string comparison by default can have potentially unintentional runtime behavior that does not match user intent. 建议将重载与参数一起使用, StringComparison 以了解目的的正确性和清晰度。It is recommended that you use the overload with the StringComparison parameter for correctness and clarity of intent.

默认情况下,此规则标记使用区域性特定值的字符串比较方法 StringComparisonThis rule flags string comparison methods that use the culture-specific StringComparison value by default. 有关详细信息,请参阅 使用当前区域性的字符串比较For more information, see String comparisons that use the current culture.


如果要查看所有字符串比较方法的冲突,而不考虑方法使用的默认字符串比较,请改用 CA1307:指定 StringComparisonIf you want to see violations for all string comparison methods, regardless of the default string comparison used by the method, please use CA1307: Specify StringComparison for clarity instead.

如何解决冲突How to fix violations

若要修复与此规则的冲突,请将字符串比较方法更改为接受 StringComparison 枚举作为参数的重载。To fix a violation of this rule, change string comparison methods to overloads that accept the StringComparison enumeration as a parameter. 例如,将 String.Compare(str1, str2) 更改为 String.Compare(str1, str2, StringComparison.Ordinal)For example, change String.Compare(str1, str2) to String.Compare(str1, str2, StringComparison.Ordinal).

何时禁止显示警告When to suppress warnings

如果库或应用程序不打算本地化,则可以安全地禁止显示此规则发出的警告。It is safe to suppress a warning from this rule when the library or application is not intended to be localized.

请参阅See also