String.IComparable.CompareTo(Object) Yöntem

Tanım

Bu örneği belirtilen bir ile karşılar ve bu örneğin belirtilen sıralama düzeninde önce mi, sonra mı yoksa aynı konumda Object mı olduğunu Object gösterir.

 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

value
Object

bir olarak değerlendirilen String nesne.

Döndürülenler

Int32

Bu örneğinden önce mi, sonra mı olduğunu veya parametreyle sıralama düzeninde aynı konumda görüntülendiğinden, 32 bitlik imzalı bir value tamsayı.

DeğerKoşul
Sıfırdan küçükBu örnek value önündedir.
SıfırBu örnek, sıralama düzeninde ile aynı konuma value sahip.
Sıfırdan büyükBu örnek , value veya value null olur.

Uygulamalar

Özel durumlar

value bir değil String .

Örnekler

Aşağıdaki örnek CompareTo yöntemi ile kullanır Object . Bir örneği bir nesneyle karşılaştırmaya çalıştığı için String TestClass Yöntem bir oluşturur ArgumentException .

using namespace System;

public ref class TestClass{};

int main()
{
   TestClass^ test = gcnew TestClass;
   array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
                                        (123).ToString(), "some text",
                                        "Some Text" };
   String^ s = "some text";
   for each (Object^ objectToCompare in objectsToCompare) {
      try {
         Int32 i = s->CompareTo(objectToCompare);
         Console::WriteLine("Comparing '{0}' with '{1}': {2}",
                            s, objectToCompare, i);
      }
      catch (ArgumentException^ e) {
         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
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
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 nesne String olması gerekir.

Dikkat

yöntemi CompareTo öncelikli olarak 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ı. İki dizenin eşdeğer olup olmadığını belirlemek için yöntemini Equals çağırma.

Bu yöntem, geçerli kültür kullanılarak bir sözcük (büyük/küçük harfe ve kültüre duyarlı) karşılaştırması gerçekleştirir. Sözcük, dize ve sıra sıralar hakkında daha fazla bilgi için bkz System.Globalization.CompareOptions ..

Bu yöntemin davranışı hakkında daha fazla bilgi için, yönteminin açıklamalar bölümüne bakın String.Compare(String, String) .

Şunlara uygulanır