String.IComparable.CompareTo(Object) Metodo

Definizione

Confronta questa istanza con un oggetto specificato Object e indica se questa istanza precede, segue o viene visualizzata nella stessa posizione nell'ordinamento dell'oggetto specificato Object.

 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

Parametri

value
Object

Oggetto che restituisce un oggetto String.

Valori restituiti

Intero con segno a 32 bit che indica se questa istanza precede, segue o viene visualizzata nella stessa posizione nell'ordinamento value del parametro .

ValueCondition
Minore di zeroQuesta istanza precede value.
ZeroQuesta istanza ha la stessa posizione nell'ordinamento di value.
Maggiore di zeroQuesta istanza segue value, o value è null.

Implementazioni

Eccezioni

value non è un oggetto String.

Esempio

Nell'esempio seguente viene utilizzato il CompareTo metodo con un oggetto Object. Poiché tenta di confrontare un'istanza String con un TestClass oggetto , il metodo genera un'eccezione 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

Commenti

value deve essere un String oggetto .

Attenzione

Il CompareTo metodo è stato progettato principalmente per l'uso nelle operazioni di ordinamento o alfabetizzazione. Non deve essere usato quando lo scopo principale della chiamata al metodo è determinare se due stringhe sono equivalenti. Per determinare se due stringhe sono equivalenti, chiamare il Equals metodo .

Questo metodo esegue un confronto di parole (con distinzione tra maiuscole e minuscole e con distinzione delle impostazioni cultura) usando le impostazioni cultura correnti. Per altre informazioni sugli ordinali di parole, string e ordinali, vedere System.Globalization.CompareOptions.

Per altre informazioni sul comportamento di questo metodo, vedere la sezione Osservazioni del String.Compare(String, String) metodo .

Si applica a