String.IComparable.CompareTo(Object) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
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
Parametreler
Döndürülenler
Bu örneğin parametreyle aynı sıralama düzeninde value önce geldiğini, izlediğini veya görüntülendiğini gösteren 32 bit imzalı tamsayı.
| Değer | Condition |
|---|---|
| Sıfırdan küçük | Bu örnek öncesindedir value. |
| Sıfır | Bu örnek, sıralama düzeninde ile valueaynı konuma sahiptir. |
| Sıfırdan büyük | Bu örnek , valueveya value şeklindedir null. |
Uygulamalar
Özel durumlar
value bir Stringdeğildir.
Örnekler
Aşağıdaki örnek ile yöntemini CompareTokullanırObject. Bir örneği bir StringTestClass nesneyle karşılaştırmaya çalıştığından, yöntemi bir ArgumentExceptionoluşturur.
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
Açıklamalar
value bir String nesne olmalıdır.
Caution
CompareTo yöntemi öncelikle sıralama veya alfabetik işlemler için tasarlanmıştır. Yöntem çağrısının birincil amacı iki dizenin eşdeğer olup olmadığını belirlemek olduğunda kullanılmamalıdır. İki dizenin eşdeğer olup olmadığını belirlemek için yöntemini çağırın Equals .
Bu yöntem, geçerli kültürü kullanarak bir sözcük (büyük/küçük harfe duyarlı ve kültüre duyarlı) karşılaştırması gerçekleştirir. Sözcük, dize ve sıralı sıralamalar hakkında daha fazla bilgi için bkz System.Globalization.CompareOptions. .
Bu yöntemin davranışı hakkında daha fazla bilgi için yöntemin Açıklamalar bölümüne String.Compare(String, String) bakın.