在集合中執行不區分文化特性的字串作業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 傳遞給比較方法的確會執行不區分文化特性的比較。Note 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

CollectionsUtil.CreateCaseInsensitiveHashTable 方法是很實用的捷徑,可用來建立 Hashtable 類別的新執行個體,而此類別會忽略字串的大小寫。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  
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.InvariantCultureFor 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