次の方法で共有


CaseInsensitiveComparer クラス

2 つのオブジェクトが等しいかどうかを比較します。比較時には文字列の大文字と小文字は区別されません。

この型のすべてのメンバの一覧については、CaseInsensitiveComparer メンバ を参照してください。

System.Object
   System.Collections.CaseInsensitiveComparer

<Serializable>
Public Class CaseInsensitiveComparer   Implements IComparer
[C#]
[Serializable]
public class CaseInsensitiveComparer : IComparer
[C++]
[Serializable]
public __gc class CaseInsensitiveComparer : public IComparer
[JScript]
public
   Serializable
class CaseInsensitiveComparer implements IComparer

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

CaseInsensitiveComparer は、文字列の大文字と小文字を区別しない比較をサポートする IComparer インターフェイスを実装しています。同様に、 CaseInsensitiveHashCodeProvider は、文字列の大文字と小文字を区別しない比較をサポートする IHashCodeProvider インターフェイスを実装しています。

Comparer クラスは IComparer インターフェイスの既定の実装で、大文字と小文字を区別する文字列比較を実行します。

Hashtable でキーとして使用されるオブジェクトは、 Object.GetHashCode メソッド (または IHashCodeProvider インターフェイス) および Object.Equals メソッド (または IComparer インターフェイス) を実装または継承している必要があります。メソッドまたはインターフェイスのどちらの実装でも、大文字と小文字の区別を同じ方法で処理する必要があります。そのように実装されていないと、 Hashtable が正しく動作しない場合があります。たとえば、 Hashtable を作成するとき、このクラスは、 CaseInsensitiveHashCodeProvider クラスか大文字と小文字を区別しない IHashCodeProvider 実装と共に使用する必要があります。

文字列比較の結果は、カルチャに応じて異なる場合があります。カルチャ固有の比較の詳細については、 System.Globalization 名前空間のトピックおよび「 国際対応アプリケーションの開発 」を参照してください。

使用例

[Visual Basic, C#, C++] 次に示すのは、大文字と小文字を区別するハッシュテーブルと、大文字と小文字を区別しないハッシュテーブルを作成するコード例です。それぞれのハッシュテーブルに含まれている要素が同じでも、両者の動作は異なることがわかります。

 
Imports System
Imports System.Collections
Imports System.Globalization

Public Class SamplesHashtable

   Public Shared Sub Main()

      ' Create a Hashtable using the default hash code provider and the default comparer.
      Dim myHT1 As New Hashtable()
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the culture of the current thread.
      Dim myHT2 As New Hashtable(New CaseInsensitiveHashCodeProvider(), New CaseInsensitiveComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the InvariantCulture.
      Dim myHT3 As New Hashtable(CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant)
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT4 As New Hashtable(New CaseInsensitiveHashCodeProvider(myCul), New CaseInsensitiveComparer(myCul))
      myHT4.Add("FIRST", "Hello")
      myHT4.Add("SECOND", "World")
      myHT4.Add("THIRD", "!")

      ' Search for a key in each hashtable.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
      Console.WriteLine("first is in myHT4: {0}", myHT4.ContainsKey("first"))

   End Sub 'Main 

End Class 'SamplesHashtable


'This code produces the following output.  Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: True
'first is in myHT4: False


[C#] 
using System;
using System.Collections;
using System.Globalization;

public class SamplesHashtable  {

   public static void Main()  {

      // Create a Hashtable using the default hash code provider and the default comparer.
      Hashtable myHT1 = new Hashtable();
      myHT1.Add("FIRST", "Hello");
      myHT1.Add("SECOND", "World");
      myHT1.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the culture of the current thread.
      Hashtable myHT2 = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
      myHT2.Add("FIRST", "Hello");
      myHT2.Add("SECOND", "World");
      myHT2.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the InvariantCulture.
      Hashtable myHT3 = new Hashtable( CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant );
      myHT3.Add("FIRST", "Hello");
      myHT3.Add("SECOND", "World");
      myHT3.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
      CultureInfo myCul = new CultureInfo( "tr-TR" );
      Hashtable myHT4 = new Hashtable( new CaseInsensitiveHashCodeProvider( myCul ), new CaseInsensitiveComparer( myCul ) );
      myHT4.Add("FIRST", "Hello");
      myHT4.Add("SECOND", "World");
      myHT4.Add("THIRD", "!");

      // Search for a key in each hashtable.
      Console.WriteLine( "first is in myHT1: {0}", myHT1.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT2: {0}", myHT2.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT3: {0}", myHT3.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT4: {0}", myHT4.ContainsKey( "first" ) );

   }

}


/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: True
first is in myHT4: False

*/


[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

int main()  {

    // Create a Hashtable using the default hash code provider and the default comparer.
    Hashtable* myHT1 = new Hashtable();
    myHT1->Add(S"FIRST", S"Hello");
    myHT1->Add(S"SECOND", S"World");
    myHT1->Add(S"THIRD", S"!");

    // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
    // based on the culture of the current thread.
    Hashtable* myHT2 = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
    myHT2->Add(S"FIRST", S"Hello");
    myHT2->Add(S"SECOND", S"World");
    myHT2->Add(S"THIRD", S"!");

    // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
    // based on the InvariantCulture.
    Hashtable* myHT3 = new Hashtable( CaseInsensitiveHashCodeProvider::DefaultInvariant, CaseInsensitiveComparer::DefaultInvariant );
    myHT3->Add(S"FIRST", S"Hello");
    myHT3->Add(S"SECOND", S"World");
    myHT3->Add(S"THIRD", S"!");

    // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
    // based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
    CultureInfo* myCul = new CultureInfo( S"tr-TR" );
    Hashtable* myHT4 = new Hashtable( new CaseInsensitiveHashCodeProvider( myCul ), new CaseInsensitiveComparer( myCul ) );
    myHT4->Add(S"FIRST", S"Hello");
    myHT4->Add(S"SECOND", S"World");
    myHT4->Add(S"THIRD", S"!");

    // Search for a key in each hashtable.
    Console::WriteLine( S"first is in myHT1: {0}", __box(myHT1->ContainsKey( S"first" )));
    Console::WriteLine( S"first is in myHT2: {0}", __box(myHT2->ContainsKey( S"first" )));
    Console::WriteLine( S"first is in myHT3: {0}", __box(myHT3->ContainsKey( S"first" )));
    Console::WriteLine( S"first is in myHT4: {0}", __box(myHT4->ContainsKey( S"first" )));

}
/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: True
first is in myHT4: False

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Collections

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

CaseInsensitiveComparer メンバ | System.Collections 名前空間 | IComparer | IComparable | Comparer | System.Globalization.CompareInfo | Thread.CurrentCulture | System.Globalization.CultureInfo | CaseInsensitiveHashCodeProvider | カルチャを認識しないコレクションの操作の実行