在数组中执行不区分区域性的字符串操作Performing Culture-Insensitive String Operations in Arrays

默认情况下,Array.SortArray.BinarySearch 方法重载使用 Thread.CurrentCulture 属性执行区域性敏感型排序。Overloads of the Array.Sort and Array.BinarySearch methods perform culture-sensitive sorts by default using the Thread.CurrentCulture property. 由于排序顺序不同,因此这些方法返回的区域性敏感型结果可能会因区域性而异。Culture-sensitive results returned by these methods can vary by culture due to differences in sort orders. 若要消除区域性敏感型行为,请使用需要使用 comparer 参数的此方法重载之一。To eliminate culture-sensitive behavior, use one of the overloads of this method that accepts a comparer parameter. comparer 参数指定要在比较数组元素时使用的 IComparer 实现。The comparer parameter specifies the IComparer implementation to use when comparing elements in the array. 对于参数,指定使用 CultureInfo.InvariantCulture 的自定义固定比较器类。For the parameter, specify a custom invariant comparer class that uses CultureInfo.InvariantCulture. 在集合中执行非区域性敏感型字符串操作主题的“使用 SortedList 类”子主题提供了自定义固定比较器类的示例。An example of a custom invariant comparer class is provided in the "Using the SortedList Class" subtopic of the Performing Culture-Insensitive String Operations in Collections topic.

备注

向比较方法传递 CultureInfo.InvariantCulture 确实会执行非区域性敏感型比较 。Passing CultureInfo.InvariantCulture to a comparison method does perform a culture-insensitive comparison. 但是,这不会导致对文件路径、注册表项、环境变量等进行非语义比较。However, it does not cause a non-linguistic comparison, for example, for file paths, registry keys, and environment variables. 也不支持基于比较结果的安全决策。Neither does it support security decisions based on the comparison result. 若要进行非语义比较或支持基于结果的安全决策,应用应使用接受 StringComparison 值的比较方法。For a non-linguistic comparison or support for result-based security decisions, the application should use a comparison method that accepts a StringComparison value. 然后,应用应传递 OrdinalThe application should then pass Ordinal.

请参阅See also