カルチャの影響を受けないコレクションの操作の実行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. CaseInsensitiveComparer クラスおよび CaseInsensitiveHashCodeProvider クラスのパラメーターなしのコンストラクターは、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. アプリケーションは StringComparison を渡します。The application should then pass StringComparison.
CaseInsensitiveComparer クラスおよび CaseInsensitiveHashCodeProvider クラスの使用Using the CaseInsensitiveComparer and CaseInsensitiveHashCodeProvider Classes
CaseInsensitiveHashCodeProvider
および CaseInsensitiveComparer
のパラメーターなしのコンストラクターは、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
を示します。これがカルチャを認識するのは、CaseInsensitiveHashCodeProvider
と CaseInsensitiveComparer
のパラメーターなしのコンストラクターを使用するためです。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);
CaseInsensitiveComparer
クラスと CaseInsensitiveHashCodeProvider
クラスを使用してカルチャを認識しない 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.InvariantCulture を指定してください。For 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. このメソッドを使用して、カルチャを認識しない Hashtable
を作成することはできません。You 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.InvariantCulture
を指定してください。For 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
パラメーターを受け取るコンストラクターの 1 つを使用して SortedList
を作成します。To 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
を使用するカスタム comparer クラスを指定してください。For the parameter, specify a custom comparer class that uses CultureInfo.InvariantCulture
to compare keys. 次の例は、カルチャを認識しないカスタム comparer クラスです。これは 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
を文字列に対して使用する際にカスタム invariant comparer を指定しないと、リストにデータが設定された後で、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
を使用するカスタム invariant comparer クラスを指定してください。For the comparer
parameter, specify a custom invariant comparer class that uses CultureInfo.InvariantCulture
. カスタム invariant comparer クラスの例は、「SortedList クラスの使用」のトピックをご覧ください。An example of a custom invariant comparer class is provided in the Using the SortedList Class topic.