在集合中执行不区分区域性的字符串操作Performing Culture-Insensitive String Operations in Collections

System.Collections 命名空间中包含默认提供区域性敏感型行为的类和成员。There are classes and members in the System.Collections namespace that provide culture-sensitive behavior by default. CaseInsensitiveComparerCaseInsensitiveHashCodeProvider 类的无参数构造函数使用 Thread.CurrentCulture 属性初始化新实例。The parameterless constructors for the CaseInsensitiveComparer and CaseInsensitiveHashCodeProvider classes initialize a new instance using the Thread.CurrentCulture property. 默认情况下,CollectionsUtil.CreateCaseInsensitiveHashtable 方法的所有重载都会使用 Thread.CurrentCulture 属性新建 Hashtable 类的实例。All overloads of the CollectionsUtil.CreateCaseInsensitiveHashtable method create a new instance of the Hashtable class using the Thread.CurrentCulture property by default. 默认情况下,ArrayList.Sort 方法重载使用 Thread.CurrentCulture 执行区域性敏感型排序。Overloads of the ArrayList.Sort method perform culture-sensitive sorts by default using Thread.CurrentCulture. 将字符串用作键时,SortedList 中的排序和查找可能会受 Thread.CurrentCulture 影响。Sorting and lookup in a SortedList can be affected by Thread.CurrentCulture when strings are used as the keys. 请按本节提供的用法建议,在 Collections 命名空间中的这些类和方法中获取不区分区域性的结果。Follow the usage recommendations provided in this section to obtain culture-insensitive results from these classes and methods in the Collections namespace.

备注

向比较方法传递 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. 然后,应用应传递 StringComparisonThe application should then pass StringComparison.

使用 CaseInsensitiveComparer 和 CaseInsensitiveHashCodeProvider 类Using the CaseInsensitiveComparer and CaseInsensitiveHashCodeProvider Classes

CaseInsensitiveHashCodeProviderCaseInsensitiveComparer 的无参数构造函数使用 Thread.CurrentCulture 来初始化类的新实例,产生区域性敏感行为。The parameterless constructors for CaseInsensitiveHashCodeProvider and CaseInsensitiveComparer initialize a new instance of the class using the Thread.CurrentCulture, resulting in culture-sensitive behavior. 下面的代码示例演示区域性敏感 Hashtable 的构造函数,因为该构造函数使用 CaseInsensitiveHashCodeProviderCaseInsensitiveComparer 的无参数构造函数。The following code example demonstrates the constructor for a Hashtable that is culture-sensitive because it uses the parameterless constructors for CaseInsensitiveHashCodeProvider and CaseInsensitiveComparer.

internalHashtable = New Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default)
internalHashtable = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);

若要使用 CaseInsensitiveComparerCaseInsensitiveHashCodeProvider 类创建非区域性敏感型 Hashtable,请使用接受 culture 参数的构造函数,初始化这些类的新实例。If you want to create a culture-insensitive Hashtable using the CaseInsensitiveComparer and CaseInsensitiveHashCodeProvider classes, initialize new instances of these classes using the constructors that accept a culture parameter. 对于 culture 参数,请指定 CultureInfo.InvariantCultureFor the culture parameter, specify CultureInfo.InvariantCulture. 下面的代码示例演示不区分区域性的 Hashtable 的构造函数。The following code example demonstrates the constructor for a culture-insensitive Hashtable.

internalHashtable = New Hashtable(New
    CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture),
    New CaseInsensitiveComparer(CultureInfo.InvariantCulture))
internalHashtable = new Hashtable(new CaseInsensitiveHashCodeProvider
    (CultureInfo.InvariantCulture),
    new CaseInsensitiveComparer(CultureInfo.InvariantCulture));

使用 CollectionsUtil.CreateCaseInsensitiveHashTable 方法Using the CollectionsUtil.CreateCaseInsensitiveHashTable Method

若要为 Hashtable 类创建一个忽略字符串大小写的新实例,CollectionsUtil.CreateCaseInsensitiveHashTable 方法是一种十分有用的快捷方式。The CollectionsUtil.CreateCaseInsensitiveHashTable method is a useful shortcut for creating a new instance of the Hashtable class that ignores the case of strings. 但是,CollectionsUtil.CreateCaseInsensitiveHashTable 方法的所有重载都区分区域性,因为这些重载使用 Thread.CurrentCulture 属性。However, all overloads of the CollectionsUtil.CreateCaseInsensitiveHashTable method are culture-sensitive because they use the Thread.CurrentCulture property. 不能使用此方法创建不区分区域性的 HashtableYou cannot create a culture-insensitive Hashtable using this method. 若要创建不区分区域性的 Hashtable,请使用接受 culture 参数的 Hashtable 构造函数。To create a culture-insensitive Hashtable, use the Hashtable constructor that accepts a culture parameter. 对于 culture 参数,请指定 CultureInfo.InvariantCultureFor the culture parameter, specify CultureInfo.InvariantCulture. 下面的代码示例演示不区分区域性的 Hashtable 的构造函数。The following code example demonstrates the constructor for a culture-insensitive Hashtable.

internalHashtable = New Hashtable(New
    CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture),
    New CaseInsensitiveComparer(CultureInfo.InvariantCulture))
internalHashtable = new Hashtable(new CaseInsensitiveHashCodeProvider
    (CultureInfo.InvariantCulture),
    new CaseInsensitiveComparer(CultureInfo.InvariantCulture));

使用 SortedList 类Using the SortedList Class

SortedList 表示键值对的集合,这些键值对按键排序,并可按照键和索引进行访问。A SortedList represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. 在使用以字符串作为键的 SortedList 时,排序和查找会受 Thread.CurrentCulture 属性的影响。When you use a SortedList where strings are the keys, the sorting and lookup can be affected by the Thread.CurrentCulture property. 若要从 SortedList 获取不区分区域性的行为,请使用一个接受 comparer 参数的构造函数来创建 SortedListTo obtain culture-insensitive behavior from a SortedList, create a SortedList using one of the constructors that accepts a comparer parameter. comparer 参数指定要在比较键时使用的 IComparer 实现。The comparer parameter specifies the IComparer implementation to use when comparing keys. 对于该参数,请指定使用 CultureInfo.InvariantCulture 来比较键的自定义比较器类。For the parameter, specify a custom comparer class that uses CultureInfo.InvariantCulture to compare keys. 下面的示例说明一个不区分区域性的自定义比较器类,可将该比较器类指定为 SortedList 构造函数的 comparer 参数。The following example illustrates a custom culture-insensitive comparer class that you can specify as the comparer parameter to a SortedList constructor.

Imports System.Collections
Imports System.Globalization

Friend Class InvariantComparer
    Implements IComparer
    Private m_compareInfo As CompareInfo
    Friend Shared [Default] As New InvariantComparer()

    Friend Sub New()
        m_compareInfo = CultureInfo.InvariantCulture.CompareInfo
    End Sub

    Public Function Compare(a As Object, b As Object) As Integer _
            Implements IComparer.Compare
        Dim sa As String = CType(a, String)
        Dim sb As String = CType(b, String)
        If Not (sa Is Nothing) And Not (sb Is Nothing) Then
            Return m_compareInfo.Compare(sa, sb)
        Else
            Return Comparer.Default.Compare(a, b)
        End If
    End Function
End Class
using System;
using System.Collections;
using System.Globalization;

internal class InvariantComparer : IComparer
{
    private CompareInfo m_compareInfo;
    internal static readonly InvariantComparer Default = new
        InvariantComparer();

    internal InvariantComparer()
    {
        m_compareInfo = CultureInfo.InvariantCulture.CompareInfo;
    }

    public int Compare(Object a, Object b)
    {
        String sa = a as String;
        String sb = b as String;
        if (sa != null && sb != null)
            return m_compareInfo.Compare(sa, sb);
        else
            return Comparer.Default.Compare(a,b);
    }
}

一般而言,如果对字符串使用 SortedList 而不指定自定义固定比较器,填充列表后,更改 Thread.CurrentCulture 会使列表失效。In general, if you use a SortedList on strings without specifying a custom invariant comparer, a change to Thread.CurrentCulture after the list has been populated can invalidate the list.

使用 ArrayList.Sort 方法Using the ArrayList.Sort Method

默认情况下,ArrayList.Sort 方法的重载使用 Thread.CurrentCulture 属性来执行区分区域性的排序。Overloads of the ArrayList.Sort method perform culture-sensitive sorts by default using the Thread.CurrentCulture property. 由于排序顺序不同,结果可能会因区域性而异。Results can vary by culture due to different sort orders. 若要消除区分区域性的行为,请使用接受 IComparer 实现的此方法的重载。To eliminate culture-sensitive behavior, use the overloads of this method that accept an IComparer implementation. 对于 comparer 参数,请指定使用 CultureInfo.InvariantCulture 的自定义固定比较器类。For the comparer 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 topic.

另请参阅See also