カルチャを認識しないコレクションの操作の実行

ビューの切り替え:
スクリプトなし
.NET Framework 開発者ガイド
カルチャを認識しないコレクションの操作の実行

System.Collections 名前空間のクラスとメソッドの動作は、既定ではカルチャを認識します。CaseInsensitiveComparer クラスと CaseInsensitiveHashCodeProvider クラスの既定のコンストラクタは、Thread.CurrentCulture プロパティを使用して新しいインスタンスを初期化します。CollectionsUtil.CreateCaseInsensitiveHashTable メソッドのすべてのオーバーロードは、既定では Thread.CurrentCulture プロパティを使用して Hashtable クラスの新しいインスタンスを作成します。ArrayList.Sort メソッドのオーバーロードは、既定では Thread.CurrentCulture を使用してカルチャに依存した並べ替えを実行します。文字列をキーとして使用する場合、SortedList の並べ替えと検索は Thread.CurrentCulture の影響を受けることがあります。Collections 名前空間のこれらのクラスやメソッドからカルチャを認識しない結果を取得するには、後で説明する使用に関する推奨事項に従ってください。

CaseInsensitiveComparer クラスと CaseInsensitiveHashCodeProvider クラスの使用

CaseInsensitiveHashCodeProvider および CaseInsensitiveComparer の既定のコンストラクタは、Thread.CurrentCulture を使用してクラスの新しいインスタンスを初期化するので、その動作ではカルチャが認識されます。CaseInsensitiveHashCodeProvider および CaseInsensitiveComparer の既定のコンストラクタを使用するためカルチャを認識する Hashtable のコンストラクタのコード例を次に示します。

Visual Basic
internalHashtable = New Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default)

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

CaseInsensitiveComparer クラスと CaseInsensitiveHashCodeProvider クラスを使用してカルチャを認識しない Hashtable を作成するには、culture パラメータを受け取るコンストラクタを使用してこれらのクラスの新しいインスタンスを初期化します。culture パラメータには、CultureInfo.InvariantCulture を指定します。カルチャを認識しない Hashtable のコード例を次に示します。

Visual Basic
internalHashtable = New Hashtable(New
    CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture),
    New CaseInsensitiveComparer(CultureInfo.InvariantCulture))

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

CollectionsUtil.CreateCaseInsensitiveHashTable メソッドの使用

CollectionsUtil.CreateCaseInsensitiveHashTable メソッドを使用すると、文字列の大文字と小文字を区別しない Hashtable クラスの新しいインスタンスを簡単に作成できます。ただし、CollectionsUtil.CreateCaseInsensitiveHashTable メソッドのオーバーロードはすべて、Thread.CurrentCulture プロパティを使用するため、カルチャを認識します。このメソッドを使用して、カルチャを認識しない Hashtable を作成することはできません。カルチャを認識しない Hashtable を作成するには、culture パラメータを受け取る Hashtable のコンストラクタを使用します。culture パラメータには、CultureInfo.InvariantCulture を指定します。カルチャを認識しない Hashtable のコード例を次に示します。

Visual Basic
internalHashtable = New Hashtable(New
    CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture),
    New CaseInsensitiveComparer(CultureInfo.InvariantCulture))

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

SortedList クラスの使用

SortedList は、キーで並べ替えられ、キーやインデックスでアクセスできるキーと値のペアのコレクションを表します。文字列がキーである場合に SortedList を使用すると、並べ替えおよび検索が Thread.CurrentCulture プロパティの影響を受ける可能性があります。SortedList からカルチャを認識しない動作を取得するには、comparer パラメータを受け取るコンストラクタを使用して SortedList を作成します。comparer パラメータは、キーを比較するときに使用する IComparer の実装を指定します。IComparer パラメータとして、CultureInfo.InvariantCulture を使用してキーを比較するカスタム比較演算子クラスを指定します。SortedList コンストラクタの IComparer パラメータとして指定できる、カルチャに依存しないカスタム比較演算子クラスの例を次に示します。

Visual Basic
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

C#
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 を変更するとリストが無効になる場合があります。

ArrayList.Sort メソッドの使用

ArrayList.Sort メソッドのオーバーロードは、既定では Thread.CurrentCulture プロパティを使用してカルチャを認識する並べ替えを実行します。結果は、並べ替えの順序が異なるため、カルチャごとに異なることがあります。カルチャに依存した動作を行わないようにするには、IComparer パラメータを受け取るこのメソッドのオーバーロードを使用します。IComparer パラメータとして、CultureInfo.InvariantCulture を使用するカスタム インバリアント比較演算子クラスを指定します。カスタム インバリアント比較演算子クラスの例については、「SortedList クラスの使用」を参照してください。

参照