CaseInsensitiveComparer Klasa

Definicja

Porównuje dwa obiekty pod kątem równoważności, ignorując przypadek ciągów.

public ref class CaseInsensitiveComparer : System::Collections::IComparer
public class CaseInsensitiveComparer : System.Collections.IComparer
[System.Serializable]
public class CaseInsensitiveComparer : System.Collections.IComparer
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CaseInsensitiveComparer : System.Collections.IComparer
type CaseInsensitiveComparer = class
    interface IComparer
[<System.Serializable>]
type CaseInsensitiveComparer = class
    interface IComparer
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CaseInsensitiveComparer = class
    interface IComparer
Public Class CaseInsensitiveComparer
Implements IComparer
Dziedziczenie
CaseInsensitiveComparer
Atrybuty
Implementuje

Przykłady

Poniższy przykład kodu tworzy tabelę skrótów uwzględniającą wielkość liter i tabelę skrótów bez uwzględniania wielkości liter i demonstruje różnicę w ich zachowaniu, nawet jeśli oba zawierają te same elementy.

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

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

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

End Class


'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

Uwagi

CaseInsensitiveComparer Implementuje IComparer interfejs obsługujący porównania bez uwzględniania wielkości liter w ciągach, podobnie jak CaseInsensitiveHashCodeProvider implementuje IHashCodeProvider interfejs obsługujący porównania bez uwzględniania wielkości liter w ciągach.

Ważne

Nie zalecamy używania CaseInsensitiveComparer klasy do tworzenia nowych aplikacji. Zamiast tego zalecamy użycie obiektu zwróconego System.StringComparer StringComparer.CurrentCultureIgnoreCaseprzez właściwość , StringComparer.InvariantCultureIgnoreCaselub StringComparer.OrdinalIgnoreCase .

Klasa Comparer jest domyślną implementacją interfejsu IComparer i wykonuje porównania ciągów z uwzględnieniem wielkości liter.

Obiekty używane jako klucze przez element Hashtable są wymagane do zastąpienia Object.GetHashCode metody (lub interfejsu IHashCodeProvider ) i Object.Equals metody (lub interfejsu IComparer ). Implementacja obu metod lub interfejsów musi obsługiwać wielkość liter w taki sam sposób; Hashtable w przeciwnym razie element może zachowywać się nieprawidłowo. Na przykład podczas tworzenia Hashtableklasy należy użyć tej klasy z klasą CaseInsensitiveHashCodeProvider lub implementacją bez uwzględniania IHashCodeProvider wielkości liter.

Porównania ciągów mogą mieć różne wyniki w zależności od kultury. Aby uzyskać więcej informacji na temat porównań specyficznych dla kultury, zobacz System.Globalization przestrzeni nazw i globalizacji i lokalizacji.

Konstruktory

CaseInsensitiveComparer()

Inicjuje nowe wystąpienie CaseInsensitiveComparer klasy przy użyciu CurrentCulture bieżącego wątku.

CaseInsensitiveComparer(CultureInfo)

Inicjuje CaseInsensitiveComparer nowe wystąpienie klasy przy użyciu określonego CultureInfoelementu .

Właściwości

Default

Pobiera wystąpienie CaseInsensitiveComparer tego elementu skojarzone z CurrentCulture bieżącym wątkiem i jest zawsze dostępne.

DefaultInvariant

Pobiera wystąpienie CaseInsensitiveComparer , które jest skojarzone z elementem InvariantCulture i jest zawsze dostępne.

Metody

Compare(Object, Object)

Wykonuje porównanie bez uwzględniania wielkości liter dwóch obiektów tego samego typu i zwraca wartość wskazującą, czy jedna jest mniejsza, równa lub większa niż druga.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też