Freigeben über


CaseInsensitiveHashCodeProvider-Klasse

HINWEIS: Diese Klasse ist mittlerweile veraltet.

Stellt einen Hashcode für ein Objekt bereit, wobei im verwendeten Hashalgorithmus die Groß- und Kleinschreibung von Zeichenfolgen ignoriert wird.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SerializableAttribute> _
<ObsoleteAttribute("Please use StringComparer instead.")> _
<ComVisibleAttribute(True)> _
Public Class CaseInsensitiveHashCodeProvider
    Implements IHashCodeProvider
'Usage
Dim instance As CaseInsensitiveHashCodeProvider
[SerializableAttribute] 
[ObsoleteAttribute("Please use StringComparer instead.")] 
[ComVisibleAttribute(true)] 
public class CaseInsensitiveHashCodeProvider : IHashCodeProvider
[SerializableAttribute] 
[ObsoleteAttribute(L"Please use StringComparer instead.")] 
[ComVisibleAttribute(true)] 
public ref class CaseInsensitiveHashCodeProvider : IHashCodeProvider
/** @attribute SerializableAttribute() */ 
/** @attribute ObsoleteAttribute("Please use StringComparer instead.") */ 
/** @attribute ComVisibleAttribute(true) */ 
public class CaseInsensitiveHashCodeProvider implements IHashCodeProvider
SerializableAttribute 
ObsoleteAttribute("Please use StringComparer instead.") 
ComVisibleAttribute(true) 
public class CaseInsensitiveHashCodeProvider implements IHashCodeProvider

Hinweise

CaseInsensitiveHashCodeProvider implementiert die IHashCodeProvider-Schnittstelle, wobei Zeichenfolgenvergleiche ohne Berücksichtigung der Groß- und Kleinschreibung auf dieselbe Weise unterstützt werden, wie CaseInsensitiveComparer die IComparer-Schnittstelle für eine Unterstützung von Zeichenfolgenvergleichen ohne Berücksichtigung der Groß- und Kleinschreibung implementiert.

Die Objekte, die von Hashtable als Schlüssel verwendet werden, müssen die Object.GetHashCode-Methode (oder die IHashCodeProvider-Schnittstelle) und die Object.Equals-Methode (oder die IComparer-Schnittstelle) überschreiben. Die Implementierungen beider Methoden bzw. Schnittstellen müssen Groß- und Kleinschreibung auf gleiche Weise behandeln, andernfalls verhält sich die Hashtable möglicherweise fehlerhaft. Beim Erstellen einer Hashtable müssen Sie diese Klasse z. B. mit der CaseInsensitiveComparer-Klasse oder mit einer beliebigen IComparer-Implementierung verwenden, die Groß- und Kleinschreibung nicht berücksichtigt.

Beispiel

Im folgenden Codebeispiel wird eine Hashtabelle mit Berücksichtigung der Groß-/Kleinschreibung und eine Hashtabelle ohne Berücksichtigung der Groß-/Kleinschreibung erstellt und der Unterschied zwischen deren Verhalten veranschaulicht, selbst wenn beide Hashtabellen dieselben Elemente enthalten.

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

*/
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 = gcnew 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 = gcnew Hashtable( gcnew CaseInsensitiveHashCodeProvider,gcnew 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 = gcnew 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 = gcnew CultureInfo( "tr-TR" );
   Hashtable^ myHT4 = gcnew Hashtable( gcnew CaseInsensitiveHashCodeProvider( myCul ),gcnew 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

*/
import System.*;
import System.Collections.*;
import System.Globalization.*;

public class SamplesHashtable
{
    public static void main(String[] args)
    {
        // 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.get_DefaultInvariant(),
            CaseInsensitiveComparer.get_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}", 
            (System.Boolean)myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", 
            (System.Boolean)myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", 
            (System.Boolean)myHT3.ContainsKey("first"));
        Console.WriteLine("first is in myHT4: {0}", 
            (System.Boolean)myHT4.ContainsKey("first"));
    } //main
} //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

*/

Vererbungshierarchie

System.Object
  System.Collections.CaseInsensitiveHashCodeProvider

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 1.0, 1.1
Veraltet (Compilerwarnung) in 2.0

.NET Compact Framework

Unterstützt in: 1.0
Veraltet (Compilerwarnung) in 2.0

Siehe auch

Referenz

CaseInsensitiveHashCodeProvider-Member
System.Collections-Namespace
Hashtable
IHashCodeProvider
Thread.CurrentCulture
System.Globalization.CultureInfo
CaseInsensitiveComparer-Klasse

Weitere Ressourcen

Durchführen kulturunabhängiger Zeichenfolgenoperationen in Auflistungen