共用方式為


在集合中執行不區分文化特性的字串作業

System.Collections 命名空間中,有類別和成員預設會提供區分文化特性的行為。 和 CaseInsensitiveComparer 類別的CaseInsensitiveHashCodeProvider無參數建構函式會使用 Thread.CurrentCulture 屬性初始化新的實例。 方法的所有多載CollectionsUtil.CreateCaseInsensitiveHashtable預設都會使用Hashtable屬性建立Thread.CurrentCulture類別的新實例。 多載版本的 ArrayList.Sort 方法預設會使用 Thread.CurrentCulture 來執行文化區分排序。 當字串作為鍵使用時,可能會影響 SortedList 中的 Thread.CurrentCulture 的排序和查詢。 請遵循本節所提供的使用建議,從命名空間中的 Collections 這些類別和方法取得不區分文化特性的結果。

備註

傳遞 CultureInfo.InvariantCulture 至比較方法會執行不區分文化特性的比較。 不過,它不會造成非語言比較,例如檔案路徑、登錄機碼和環境變數。 這兩者都不支援以比較結果為基礎的安全性決策。 針對非語言比較或支援以結果為基礎的安全性決策,應用程式應該使用接受 StringComparison 值的比較方法。 然後,應用程式應該會傳遞 StringComparison

使用CaseInsensitiveComparerCaseInsensitiveHashCodeProvider類別

CaseInsensitiveHashCodeProviderCaseInsensitiveComparer 的無參數建構函式使用 Thread.CurrentCulture 初始化一個新的類別實例,從而產生區分文化特性的行為。 下列程式碼範例示範了Hashtable的建構函式,這是一個與文化相關的函式,因為它使用了CaseInsensitiveHashCodeProviderCaseInsensitiveComparer的無參數建構函式。

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

如果您想要使用 HashtableCaseInsensitiveComparer 類別建立不受CaseInsensitiveHashCodeProvider文化特性影響的項目,請使用接受culture參數的建構函式來初始化這些類別的新實例。 針對culture參數,指定CultureInfo.InvariantCulture。 下列程式代碼範例示範與語言文化無關的 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方法

CollectionsUtil.CreateCaseInsensitiveHashTable 方法是建立 Hashtable 類別的新實例的實用快捷方式,該類別可忽略字串的大小寫。 不過,由於會使用 CollectionsUtil.CreateCaseInsensitiveHashTable 屬性,方法的所有多載都對文化敏感。 您無法使用此方法建立不區分文化特性 Hashtable 。 若要建立不區分文化特性的 Hashtable,請使用接受 Hashtable 參數的 culture 建構函式。 針對culture參數,指定CultureInfo.InvariantCulture。 下列程式代碼範例示範與語言文化無關的 Hashtable 建構函式。

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

使用 SortedList 類別

SortedList 表示一組鍵值對,這些鍵值對依鍵進行排序,可透過鍵和索引存取。 當您使用 SortedList 以字串為鍵的情況下,排序和查找可能會受到 Thread.CurrentCulture 屬性的影響。 若要從 SortedList取得不區分文化特性的行為,請使用接受 SortedList 參數的其中一個建構函式來建立 comparer 。 參數 comparer 指定用於比較索引鍵的實作 IComparer。 針對該參數,指定一個使用 CultureInfo.InvariantCulture 來比較鍵的自定義比較器類別。 您可以將下列範例中說明的自訂不區分文化的比較器類別指定為 comparer 建構函式的 SortedList 參數。

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 _compareInfo;
    internal static readonly InvariantComparer Default = new
        InvariantComparer();

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

    public int Compare(Object a, Object b)
    {
        if (a is string sa && b is string sb)
            return _compareInfo.Compare(sa, sb);
        else
            return Comparer.Default.Compare(a,b);
    }
}

一般而言,如果您在字串上使用某種 SortedList 而不指定自定義不變比較子,則在清單填入後進行的變更 Thread.CurrentCulture 可能會使清單失效。

使用ArrayList.Sort方法

多載的 ArrayList.Sort 方法預設會使用 Thread.CurrentCulture 屬性來執行區分文化特性的排序。 由於不同的排序順序,結果可能會因文化特性而異。 若要消除與文化有關的行為,請使用接受 IComparer 實作的此方法多載。 針對comparer參數,指定一個使用CultureInfo.InvariantCulture的自定義非變異比較器類別。 使用 SortedList 類別主題中提供了自訂不變比較子類別的範例。

另請參閱