Freigeben über


Comparer-Konstruktor

Initialisiert eine neue Instanz der Comparer-Klasse mit der angegebenen System.Globalization.CultureInfo.

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

Syntax

'Declaration
Public Sub New ( _
    culture As CultureInfo _
)
'Usage
Dim culture As CultureInfo

Dim instance As New Comparer(culture)
public Comparer (
    CultureInfo culture
)
public:
Comparer (
    CultureInfo^ culture
)
public Comparer (
    CultureInfo culture
)
public function Comparer (
    culture : CultureInfo
)

Parameter

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

culture ist NULL (Nothing in Visual Basic).

Hinweise

Vergleichsprozeduren verwenden die angegebene System.Globalization.CultureInfo, um die Sortierreihenfolge und die Regeln für die Groß- und Kleinschreibung zu bestimmen. Vergleiche von Zeichenfolgen können je nach Kultur zu verschiedenen Ergebnissen führen. Weitere Informationen über kulturabhängige Vergleiche finden Sie unter dem System.Globalization-Namespace und unter Codierung und Lokalisierung.

Beispiel

Im folgenden Beispiel wird veranschaulicht, wie Compare in Abhängigkeit von der dem Comparer zugeordneten Kultur unterschiedliche Werte zurückgibt.

Imports System
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 'Main 

End Class 'SamplesComparer


'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

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

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

public class SamplesComparer
{
    public static void main(String[] args)
    {
        // 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}", 
            (Int32)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}", 
            (Int32)myCompIntl.Compare(str1, str2));

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

/*
This code produces the following output.

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

*/

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: 2.0, 1.1

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Comparer-Klasse
Comparer-Member
System.Collections-Namespace
System.Globalization.CultureInfo
System.Globalization.CompareInfo