Export (0) Print
Expand All

CaseInsensitiveHashCodeProvider Class

Supplies a hash code for an object, using a hashing algorithm that ignores the case of strings.

For a list of all members of this type, see CaseInsensitiveHashCodeProvider Members.

System.Object
   System.Collections.CaseInsensitiveHashCodeProvider

[Visual Basic]
<Serializable>
Public Class CaseInsensitiveHashCodeProvider
   Implements IHashCodeProvider
[C#]
[Serializable]
public class CaseInsensitiveHashCodeProvider : IHashCodeProvider
[C++]
[Serializable]
public __gc class CaseInsensitiveHashCodeProvider : public
   IHashCodeProvider
[JScript]
public
   Serializable
class CaseInsensitiveHashCodeProvider implements
   IHashCodeProvider

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

CaseInsensitiveHashCodeProvider implements the IHashCodeProvider interface supporting case-insensitive comparisons on strings, just as CaseInsensitiveComparer implements the IComparer interface supporting case-insensitive comparisons on strings.

The objects used as keys by a Hashtable are required to implement or inherit the Object.GetHashCode method (or the IHashCodeProvider interface) and the Object.Equals method (or the IComparer interface). The implementation of both methods or interfaces must handle case sensitivity the same way; otherwise, the Hashtable might behave incorrectly. For example, when creating a Hashtable, you must use this class with the CaseInsensitiveComparer class or any case-insensitive IComparer implementation.

Example

[Visual Basic, C#, C++] The following code example creates a case-sensitive hashtable and a case-insensitive hashtable and demonstrates the difference in their behavior, even if both contain the same elements.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

CaseInsensitiveHashCodeProvider Members | System.Collections Namespace | Hashtable | IHashCodeProvider | Thread.CurrentCulture | System.Globalization.CultureInfo | CaseInsensitiveComparer | Performing Culture-Insensitive String Operations in Collections

Show:
© 2014 Microsoft