String.IComparable.CompareTo(Object) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
virtual int System.IComparable.CompareTo(System::Object ^ value) = IComparable::CompareTo;
int IComparable.CompareTo(object value);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo
Parameter
Gibt zurück
Eine 32-Bit-Ganzzahl, die angibt, ob diese Instanz vor, folgt oder in derselben Position in der Sortierreihenfolge wie der value Parameter angezeigt wird.
| Wert | Zustand |
|---|---|
| Kleiner als null | Diese Instanz steht vor value. |
| Zero | Diese Instanz hat die gleiche Position in der Sortierreihenfolge wie value. |
| Größer als null | Diese Instanz folgt valueoder value ist null. |
Implementiert
Ausnahmen
value ist kein String.
Beispiele
Im folgenden Beispiel wird die CompareTo Methode mit einer Object. Da versucht wird, eine String Instanz mit einem TestClass Objekt zu vergleichen, löst die Methode eine ArgumentException.
using System;
public class TestClass
{}
public class Example
{
public static void Main()
{
var test = new TestClass();
Object[] objectsToCompare = { test, test.ToString(), 123,
123.ToString(), "some text",
"Some Text" };
string s = "some text";
foreach (var objectToCompare in objectsToCompare) {
try {
int i = s.CompareTo(objectToCompare);
Console.WriteLine("Comparing '{0}' with '{1}': {2}",
s, objectToCompare, i);
}
catch (ArgumentException) {
Console.WriteLine("Bad argument: {0} (type {1})",
objectToCompare,
objectToCompare.GetType().Name);
}
}
}
}
// The example displays the following output:
// Bad argument: TestClass (type TestClass)
// Comparing 'some text' with 'TestClass': -1
// Bad argument: 123 (type Int32)
// Comparing 'some text' with '123': 1
// Comparing 'some text' with 'some text': 0
// Comparing 'some text' with 'Some Text': -1
open System
type TestClass() = class end
let test = TestClass()
let objectsToCompare: obj list =
[ test; string test; 123
string 123; "some text"
"Some Text" ]
let s = "some text"
for objectToCompare in objectsToCompare do
try
let i = s.CompareTo objectToCompare
printfn $"Comparing '{s}' with '{objectToCompare}': {i}"
with :? ArgumentException ->
printfn $"Bad argument: {objectToCompare} (type {objectToCompare.GetType().Name})"
// The example displays the following output:
// Bad argument: TestClass (type TestClass)
// Comparing 'some text' with 'TestClass': -1
// Bad argument: 123 (type Int32)
// Comparing 'some text' with '123': 1
// Comparing 'some text' with 'some text': 0
// Comparing 'some text' with 'Some Text': -1
Public Class TestClass
End Class
Public Class Example
Public Shared Sub Main()
Dim test As New TestClass()
Dim objectsToCompare() As Object = { test, test.ToString(), 123,
123.ToString(), "some text",
"Some Text" }
Dim s As String = "some text"
For Each objectToCompare In objectsToCompare
Try
Dim i As Integer = s.CompareTo(objectToCompare)
Console.WriteLine("Comparing '{0}' with '{1}': {2}",
s, objectToCompare, i)
Catch e As ArgumentException
Console.WriteLine("Bad argument: {0} (type {1})",
objectToCompare,
objectToCompare.GetType().Name)
End Try
Next
End Sub
End Class
' The example displays the following output:
' Bad argument: TestClass (type TestClass)
' Comparing 'some text' with 'TestClass': -1
' Bad argument: 123 (type Int32)
' Comparing 'some text' with '123': 1
' Comparing 'some text' with 'some text': 0
' Comparing 'some text' with 'Some Text': -1
Hinweise
value muss ein String Objekt sein.
Vorsicht
Die CompareTo Methode wurde hauptsächlich für Sortier- oder Alphabetisierungsvorgänge entwickelt. Sie sollte nicht verwendet werden, wenn der Hauptzweck des Methodenaufrufs darin besteht, zu bestimmen, ob zwei Zeichenfolgen gleichwertig sind. Um zu ermitteln, ob zwei Zeichenfolgen gleichwertig sind, rufen Sie die Equals Methode auf.
Diese Methode führt einen Vergleich zwischen Groß- und Kleinschreibung und Kultur mithilfe der aktuellen Kultur durch. Weitere Informationen zu Wort-, Zeichenfolgen- und Ordnungssortierungen finden Sie unter System.Globalization.CompareOptions.
Weitere Informationen zum Verhalten dieser Methode finden Sie im Abschnitt "Hinweise" der String.Compare(String, String) Methode.