CaseInsensitiveComparer CaseInsensitiveComparer CaseInsensitiveComparer CaseInsensitiveComparer Class

Definizione

Consente di confrontare due oggetti in termini di uguaglianza, ignorando la distinzione tra maiuscole e minuscole nelle stringhe.Compares two objects for equivalence, ignoring the case of strings.

public ref class CaseInsensitiveComparer : System::Collections::IComparer
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CaseInsensitiveComparer : System.Collections.IComparer
type CaseInsensitiveComparer = class
    interface IComparer
Public Class CaseInsensitiveComparer
Implements IComparer
Ereditarietà
CaseInsensitiveComparerCaseInsensitiveComparerCaseInsensitiveComparerCaseInsensitiveComparer
Attributi
Implementazioni

Esempi

Nell'esempio di codice seguente vengono create una tabella hash con distinzione tra maiuscole e minuscole e una tabella hash senza distinzione tra maiuscole e minuscole e viene illustrata la differenza nel loro comportamento, anche se entrambi contengono gli stessi elementi.The following code example creates a case-sensitive hash table and a case-insensitive hash table and demonstrates the difference in their behavior, even if both contain the same elements.

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

Commenti

CaseInsensitiveComparerimplementa l' IComparer interfaccia che supporta confronti senza distinzione tra maiuscole e minuscole nelle IHashCodeProvider stringhe, così come implementa l'interfaccia che CaseInsensitiveHashCodeProvider supporta confronti senza distinzione tra maiuscole e minuscole nelle stringhe.CaseInsensitiveComparer implements the IComparer interface supporting case-insensitive comparisons on strings, just as CaseInsensitiveHashCodeProvider implements the IHashCodeProvider interface supporting case-insensitive comparisons on strings.

Importante

Non è consigliabile usare la CaseInsensitiveComparer classe per nuove attività di sviluppo.We don't recommend that you use the CaseInsensitiveComparer class for new development. È invece System.StringComparer consigliabile utilizzare l'oggetto restituito StringComparer.CurrentCultureIgnoreCasedalla proprietà, StringComparer.InvariantCultureIgnoreCaseo StringComparer.OrdinalIgnoreCase .Instead, we recommend that you use the System.StringComparer object returned by the StringComparer.CurrentCultureIgnoreCase, StringComparer.InvariantCultureIgnoreCase, or StringComparer.OrdinalIgnoreCase property.

La Comparer classe è l'implementazione predefinita IComparer dell'interfaccia ed esegue confronti tra stringhe con distinzione tra maiuscole e minuscole.The Comparer class is the default implementation of the IComparer interface and performs case-sensitive string comparisons.

Gli oggetti utilizzati come chiavi da un Hashtable oggetto sono necessari per eseguire Object.GetHashCode l'override del metodo IHashCodeProvider o dell'interfaccia e Object.Equals del metodo o dell' IComparer interfaccia.The objects used as keys by a Hashtable are required to override the Object.GetHashCode method (or the IHashCodeProvider interface) and the Object.Equals method (or the IComparer interface). L'implementazione di metodi o interfacce deve gestire la distinzione tra maiuscole e minuscole nello stesso modo; in caso contrario Hashtable , il comportamento potrebbe non essere corretto.The implementation of both methods or interfaces must handle case sensitivity the same way; otherwise, the Hashtable might behave incorrectly. Ad esempio, quando si crea Hashtableun oggetto, è necessario usare questa classe CaseInsensitiveHashCodeProvider con la classe o qualsiasi implementazione senza IHashCodeProvider distinzione tra maiuscole e minuscole.For example, when creating a Hashtable, you must use this class with the CaseInsensitiveHashCodeProvider class or any case-insensitive IHashCodeProvider implementation.

I confronti tra stringhe possono avere risultati diversi a seconda delle impostazioni cultura.String comparisons might have different results depending on the culture. Per ulteriori informazioni sui confronti specifici delle impostazioni cultura, System.Globalization vedere lo spazio dei nomi e la globalizzazione e localizzazione.For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

Costruttori

CaseInsensitiveComparer() CaseInsensitiveComparer() CaseInsensitiveComparer() CaseInsensitiveComparer()

Inizializza una nuova istanza della classe CaseInsensitiveComparer utilizzando la proprietà CurrentCulture del thread corrente.Initializes a new instance of the CaseInsensitiveComparer class using the CurrentCulture of the current thread.

CaseInsensitiveComparer(CultureInfo) CaseInsensitiveComparer(CultureInfo) CaseInsensitiveComparer(CultureInfo) CaseInsensitiveComparer(CultureInfo)

Inizializza una nuova istanza della classe CaseInsensitiveComparer usando il CultureInfo specificato.Initializes a new instance of the CaseInsensitiveComparer class using the specified CultureInfo.

Proprietà

Default Default Default Default

Ottiene un'istanza di CaseInsensitiveComparer associata alla proprietà CurrentCulture del thread corrente e sempre disponibile.Gets an instance of CaseInsensitiveComparer that is associated with the CurrentCulture of the current thread and that is always available.

DefaultInvariant DefaultInvariant DefaultInvariant DefaultInvariant

Ottiene un'istanza di CaseInsensitiveComparer associata alla proprietà InvariantCulture e sempre disponibile.Gets an instance of CaseInsensitiveComparer that is associated with InvariantCulture and that is always available.

Metodi

Compare(Object, Object) Compare(Object, Object) Compare(Object, Object) Compare(Object, Object)

Esegue un confronto senza distinzione tra maiuscole e minuscole tra due oggetti dello stesso tipo e viene restituito un valore che indica se uno degli oggetti è minore, uguale o maggiore dell'altro.Performs a case-insensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche