Comparer Klasa

Definicja

Porównuje dwa obiekty równoważności, gdzie porównania ciągów są uwzględniane w wielkości liter.

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

Przykłady

Poniższy przykład kodu pokazuje, jak Compare zwraca różne wartości w zależności od kultury skojarzonej z elementem Comparer.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
   
   // Creates the strings to compare.
   String^ str1 = "llegar";
   String^ str2 = "lugar";
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );
   
   // Uses the DefaultInvariant Comparer.
   Console::WriteLine( "   Invariant Comparer: {0}", Comparer::DefaultInvariant->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
   Comparer^ myCompIntl = gcnew Comparer( gcnew CultureInfo(  "es-ES",false ) );
   Console::WriteLine( "   International Sort: {0}", myCompIntl->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
   Comparer^ myCompTrad = gcnew Comparer( gcnew CultureInfo( 0x040A,false ) );
   Console::WriteLine( "   Traditional Sort  : {0}", myCompTrad->Compare( str1, str2 ) );
}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
using System;
using System.Collections;
using System.Globalization;

public class SamplesComparer  {

   public static void Main()  {

      // Creates the strings to compare.
      String str1 = "llegar";
      String str2 = "lugar";
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );

      // Uses the DefaultInvariant Comparer.
      Console.WriteLine( "   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) );
      Console.WriteLine( "   International Sort: {0}", myCompIntl.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) );
      Console.WriteLine( "   Traditional Sort  : {0}", myCompTrad.Compare( str1, str2 ) );
   }
}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
Imports System.Collections
Imports System.Globalization

Public Class SamplesComparer

   Public Shared Sub Main()

      ' Creates the strings to compare.
      Dim str1 As [String] = "llegar"
      Dim str2 As [String] = "lugar"
      Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2)

      ' Uses the DefaultInvariant Comparer.
      Console.WriteLine("   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2))

      ' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False))
      Console.WriteLine("   International Sort: {0}", myCompIntl.Compare(str1, str2))

      ' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False))
      Console.WriteLine("   Traditional Sort  : {0}", myCompTrad.Compare(str1, str2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
'   Invariant Comparer: -1
'   International Sort: -1
'   Traditional Sort  : 1

Uwagi

Ta klasa jest domyślną implementacją interfejsu IComparer . Klasa to implementacja CaseInsensitiveComparer interfejsu IComparer , który wykonuje porównania ciągów bez uwzględniania wielkości liter. System.Collections.Generic.Comparer<T> jest ogólnym odpowiednikiem tej klasy.

Procedury porównania używają Thread.CurrentCulture bieżącego wątku, chyba że określono inaczej. 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 przestrzeń nazw i globalizację i lokalizację.

Konstruktory

Comparer(CultureInfo)

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

Pola

Default

Reprezentuje wystąpienie, Comparer które jest skojarzone z CurrentCulture bieżącym wątkiem. To pole jest tylko do odczytu.

DefaultInvariant

Reprezentuje wystąpienie Comparer , które jest skojarzone z InvariantCulture. To pole jest tylko do odczytu.

Metody

Compare(Object, Object)

Wykonuje porównanie wielkości liter dwóch obiektów tego samego typu i zwraca wartość wskazującą, czy jedna jest mniejsza niż, równa, czy 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)
GetObjectData(SerializationInfo, StreamingContext)

SerializationInfo Wypełnia obiekt danymi wymaganymi do serializacji.

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ż