System-Namespace


.NET Framework-Klassenbibliothek
Comparison<(Of <(T>)>)-Delegat

Aktualisiert: November 2007

Stellt die Methode dar, die zwei Objekte desselben Typs vergleicht.

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

Visual Basic (Deklaration)
Public Delegate Function Comparison(Of T) ( _
    x As T, _
    y As T _
) As Integer
Visual Basic (Verwendung)
Dim instance As New Comparison(Of T)(AddressOf HandlerMethod)
C#
public delegate int Comparison<T>(
    T x,
    T y
)
VisualC++
generic<typename T>
public delegate int Comparison(
    T x, 
    T y
)
J#
J# unterstützt die Verwendung von generischen APIs, aber nicht die Deklaration von neuen generischen APIs.
Jscript
JScript unterstützt keine generischen Typen oder Methoden.

Typparameter

T

Der Typ der zu vergleichenden Objekte.

Parameter

x
Typ: T
Das erste zu vergleichende Objekt.
y
Typ: T
Das zweite zu vergleichende Objekt.

Rückgabewert

Typ: System..::.Int32

Wert

Bedingung

Kleiner als 0

x ist kleiner als y.

0

x ist gleich y.

Größer als 0

x ist größer als y.

Hinweise

Dieser Delegat wird von der Sort<(Of <(T>)>)(array<T>[]()[], Comparison<(Of <(T>)>))-Methodenüberladung der Array-Klasse und der Sort(Comparison<(Of <(T>)>))-Methodenüberladung der List<(Of <(T>)>)-Klasse verwendet, um die Elemente eines Arrays oder einer Liste zu sortieren.

Beispiele

Im folgenden Codebeispiel wird die Verwendung des Comparison<(Of <(T>)>)-Delegaten mit der Sort(Comparison<(Of <(T>)>))-Methodenüberladung veranschaulicht.

Im Codebeispiel wird eine alternative Vergleichsmethode für Zeichenfolgen mit der Bezeichnung CompareDinosByLength definiert. Dieses Verfahren funktioniert folgendermaßen: Zunächst werden die zu vergleichenden Objekte auf nullNothingnullptrNULL-Verweis (Nothing in Visual Basic) getestet, und ein NULL-Verweis wird als kleiner angesehen als ein Nicht-NULL-Verweis. Dann werden die Zeichenfolgenlängen verglichen, und die längere Zeichenfolge wird als größer betrachtet. In einem dritten Schritt wird der gewöhnliche Zeichenfolgenvergleich verwendet, wenn die Längen übereinstimmen.

Es wird eine List<(Of <(T>)>) von Zeichenfolgen erstellt und ohne bestimmte Reihenfolge mit vier Zeichenfolgen aufgefüllt. Die Liste enthält auch eine leere Zeichenfolge und einen NULL-Verweis. Die Liste wird angezeigt, mithilfe eines generischen Comparison<(Of <(T>)>)-Delegaten, der die CompareDinosByLength-Methode darstellt, sortiert und erneut angezeigt.

Visual Basic
Imports System
Imports System.Collections.Generic

Public Class Example

    Private Shared Function CompareDinosByLength( _
        ByVal x As String, ByVal y As String) As Integer

        If x Is Nothing Then
            If y Is Nothing Then 
                ' If x is Nothing and y is Nothing, they're
                ' equal. 
                Return 0
            Else
                ' If x is Nothing and y is not Nothing, y
                ' is greater. 
                Return -1
            End If
        Else
            ' If x is not Nothing...
            '
            If y Is Nothing Then
                ' ...and y is Nothing, x is greater.
                Return 1
            Else
                ' ...and y is not Nothing, compare the 
                ' lengths of the two strings.
                '
                Dim retval As Integer = _
                    x.Length.CompareTo(y.Length)

                If retval <> 0 Then 
                    ' If the strings are not of equal length,
                    ' the longer string is greater.
                    '
                    Return retval
                Else
                    ' If the strings are of equal length,
                    ' sort them with ordinary string comparison.
                    '
                    Return x.CompareTo(y)
                End If
            End If
        End If

    End Function

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)
        dinosaurs.Add("Pachycephalosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("")
        dinosaurs.Add(Nothing)
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Deinonychus")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "Sort with generic Comparison(Of String) delegate:")
        dinosaurs.Sort(AddressOf CompareDinosByLength)
        Display(dinosaurs)

    End Sub

    Private Shared Sub Display(ByVal lis As List(Of String))
        Console.WriteLine()
        For Each s As String In lis
            If s Is Nothing Then
                Console.WriteLine("(Nothing)")
            Else
                Console.WriteLine("""{0}""", s)
            End If
        Next
    End Sub
End Class

' This code example produces the following output:
'
'"Pachycephalosaurus"
'"Amargasaurus"
'""
'(Nothing)
'"Mamenchisaurus"
'"Deinonychus"
'
'Sort with generic Comparison(Of String) delegate:
'
'(Nothing)
'""
'"Deinonychus"
'"Amargasaurus"
'"Mamenchisaurus"
'"Pachycephalosaurus"
C#
using System;
using System.Collections.Generic;

public class Example
{
    private static int CompareDinosByLength(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're
                // equal. 
                return 0;
            }
            else
            {
                // If x is null and y is not null, y
                // is greater. 
                return -1;
            }
        }
        else
        {
            // If x is not null...
            //
            if (y == null)
                // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {
                // ...and y is not null, compare the 
                // lengths of the two strings.
                //
                int retval = x.Length.CompareTo(y.Length);

                if (retval != 0)
                {
                    // If the strings are not of equal length,
                    // the longer string is greater.
                    //
                    return retval;
                }
                else
                {
                    // If the strings are of equal length,
                    // sort them with ordinary string comparison.
                    //
                    return x.CompareTo(y);
                }
            }
        }
    }

    public static void Main()
    {
        List<string> dinosaurs = new List<string>();
        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("");
        dinosaurs.Add(null);
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");
        Display(dinosaurs);

        Console.WriteLine("\nSort with generic Comparison<string> delegate:");
        dinosaurs.Sort(CompareDinosByLength);
        Display(dinosaurs);

    }

    private static void Display(List<string> list)
    {
        Console.WriteLine();
        foreach( string s in list )
        {
            if (s == null)
                Console.WriteLine("(null)");
            else
                Console.WriteLine("\"{0}\"", s);
        }
    }
}

/* This code example produces the following output:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison<string> delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
 */
VisualC++
using namespace System;
using namespace System::Collections::Generic;

int CompareDinosByLength(String^ x, String^ y)
{
    if (x == nullptr)
    {
        if (y == nullptr)
        {
            // If x is null and y is null, they're
            // equal. 
            return 0;
        }
        else
        {
            // If x is null and y is not null, y
            // is greater. 
            return -1;
        }
    }
    else
    {
        // If x is not null...
        //
        if (y == nullptr)
            // ...and y is null, x is greater.
        {
            return 1;
        }
        else
        {
            // ...and y is not null, compare the 
            // lengths of the two strings.
            //
            int retval = x->Length.CompareTo(y->Length);

            if (retval != 0)
            {
                // If the strings are not of equal length,
                // the longer string is greater.
                //
                return retval;
            }
            else
            {
                // If the strings are of equal length,
                // sort them with ordinary string comparison.
                //
                return x->CompareTo(y);
            }
        }
    }
};

void Display(List<String^>^ list)
{
    Console::WriteLine();
    for each(String^ s in list)
    {
        if (s == nullptr)
            Console::WriteLine("(null)");
        else
            Console::WriteLine("\"{0}\"", s);
    }
};

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>();
    dinosaurs->Add("Pachycephalosaurus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("");
    dinosaurs->Add(nullptr);
    dinosaurs->Add("Mamenchisaurus");
    dinosaurs->Add("Deinonychus");
    Display(dinosaurs);

    Console::WriteLine("\nSort with generic Comparison<String^> delegate:");
    dinosaurs->Sort(
        gcnew Comparison<String^>(CompareDinosByLength));
    Display(dinosaurs);

}

/* This code example produces the following output:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison<String^> delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
 */
Plattformen

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile für Smartphone, Windows Mobile für Pocket PC, Xbox 360

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
Versionsinformationen

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0

.NET Compact Framework

Unterstützt in: 3.5, 2.0

XNA Framework

Unterstützt in: 2.0, 1.0
Siehe auch

Referenz

Tags :


Page view tracker