Int32.CompareTo Int32.CompareTo Int32.CompareTo Int32.CompareTo Method

定義

將這個執行個體與指定的物件或 Int32 相比較,並傳回它們的相對值指示。Compares this instance to a specified object or Int32 and returns an indication of their relative values.

多載

CompareTo(Int32) CompareTo(Int32) CompareTo(Int32) CompareTo(Int32)

將這個執行個體與指定的 32 元帶正負號的整數相比較,並傳回它們的相對值指示。Compares this instance to a specified 32-bit signed integer and returns an indication of their relative values.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。Compares this instance to a specified object and returns an indication of their relative values.

CompareTo(Int32) CompareTo(Int32) CompareTo(Int32) CompareTo(Int32)

將這個執行個體與指定的 32 元帶正負號的整數相比較,並傳回它們的相對值指示。Compares this instance to a specified 32-bit signed integer and returns an indication of their relative values.

public:
 virtual int CompareTo(int value);
public int CompareTo (int value);
abstract member CompareTo : int -> int
override this.CompareTo : int -> int
Public Function CompareTo (value As Integer) As Integer

參數

value
Int32 Int32 Int32 Int32

要比較的整數。An integer to compare.

傳回

帶正負號的數字,指出這個執行個體與 value 的相對值。A signed number indicating the relative values of this instance and value.

傳回值Return Value 描述Description
小於零Less than zero 這個執行個體小於 valueThis instance is less than value.
Zero 這個執行個體等於 valueThis instance is equal to value.
大於零Greater than zero 這個執行個體大於 valueThis instance is greater than value.

實作

範例

下列範例示範Int32.CompareTo(Int32)方法。The following example demonstrates the Int32.CompareTo(Int32) method. 除了顯示四個不同的比較結果的方法所傳回的值,它會將傳回值的自訂成員Comparison列舉型別,它也會顯示其值。In addition to displaying the value returned by the method for four different comparisons, it converts the return value to a member of the custom Comparison enumeration, whose value it also displays.

using System;

enum Comparison {
   LessThan=-1, Equal=0, GreaterThan=1};

public class ValueComparison
{
   public static void Main()
   {
      int mainValue = 16325;
      int zeroValue = 0;
      int negativeValue = -1934;
      int positiveValue = 903624;
      int sameValue = 16325;
         
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).",  
                        mainValue, zeroValue, 
                        mainValue.CompareTo(zeroValue), 
                        (Comparison) mainValue.CompareTo(zeroValue));
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).",
                        mainValue, sameValue, 
                        mainValue.CompareTo(sameValue), 
                        (Comparison) mainValue.CompareTo(sameValue));
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", 
                        mainValue, negativeValue, 
                        mainValue.CompareTo(negativeValue), 
                        (Comparison) mainValue.CompareTo(negativeValue));
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", 
                        mainValue, positiveValue, 
                        mainValue.CompareTo(positiveValue), 
                        (Comparison) mainValue.CompareTo(positiveValue));
   }
}
// The example displays the following output:
//       Comparing 16325 and 0: 1 (GreaterThan).
//       Comparing 16325 and 16325: 0 (Equal).
//       Comparing 16325 and -1934: 1 (GreaterThan).
//       Comparing 16325 and 903624: -1 (LessThan).
Public Enum Comparison As Integer
   LessThan = -1
   Equal = 0
   GreaterThan = 1
End Enum

Module ValueComparison
   Public Sub Main()
      Dim mainValue As Integer = 16325
      Dim zeroValue As Integer = 0
      Dim negativeValue As Integer = -1934
      Dim positiveValue As Integer = 903624
      Dim sameValue As Integer = 16325
      
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", _ 
                        mainValue, zeroValue, _
                        mainValue.CompareTo(zeroValue), _
                        CType(mainValue.CompareTo(zeroValue), Comparison))
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", _ 
                        mainValue, sameValue, _
                        mainValue.CompareTo(sameValue), _
                        CType(mainValue.CompareTo(sameValue), Comparison))
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", _ 
                        mainValue, negativeValue, _
                        mainValue.CompareTo(negativeValue), _
                        CType(mainValue.CompareTo(negativeValue), Comparison))
                        
      Console.WriteLine("Comparing {0} and {1}: {2} ({3}).", _ 
                        mainValue, positiveValue, _
                        mainValue.CompareTo(positiveValue), _
                        CType(mainValue.CompareTo(positiveValue), Comparison))
   End Sub
End Module
' The example displays the following output:
'       Comparing 16325 and 0: 1 (GreaterThan).
'       Comparing 16325 and 16325: 0 (Equal).
'       Comparing 16325 and -1934: 1 (GreaterThan).
'       Comparing 16325 and 903624: -1 (LessThan).

備註

這個方法會實作System.IComparable<T>介面並執行稍微優於Int32.CompareTo方法因為並沒有將value物件的參數。This method implements the System.IComparable<T> interface and performs slightly better than the Int32.CompareTo method because it does not have to convert the value parameter to an object.

根據您的程式語言,可能是程式碼可能CompareTo方法的參數類型具有較少的位元的位置 (較窄) 與執行個體類型。Depending on your programming language, it might be possible to code a CompareTo method where the parameter type has fewer bits (is narrower) than the instance type. 這可能是因為有些程式語言會執行隱含的擴展轉換,將參數表示為型別具有相同位元數與執行個體。This is possible because some programming languages perform an implicit widening conversion that represents the parameter as a type with as many bits as the instance.

例如,假設此執行個體類型是Int32,且參數類型為ByteFor example, suppose the instance type is Int32 and the parameter type is Byte. Microsoft C# 編譯器會產生代表做為參數的值的指示Int32,然後產生Int32.CompareTo比較的值的方法Int32執行個體和Int32參數表示法。The Microsoft C# compiler generates instructions to represent the value of the parameter as an Int32, then generates a Int32.CompareTo method that compares the values of the Int32 instance and the Int32 parameter representation.

請參閱您的程式語言的文件,以判斷其編譯器是否會執行隱含的擴展轉換數值類型上。Consult your programming language's documentation to determine whether its compiler performs implicit widening conversions on numeric types.

另請參閱

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。Compares this instance to a specified object and returns an indication of their relative values.

public:
 virtual int CompareTo(System::Object ^ value);
public int CompareTo (object value);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
Public Function CompareTo (value As Object) As Integer

參數

value
Object Object Object Object

要比較的物件或 nullAn object to compare, or null.

傳回

帶正負號的數字,指出這個執行個體與 value 的相對值。A signed number indicating the relative values of this instance and value.

傳回值Return Value 描述Description
小於零Less than zero 這個執行個體小於 valueThis instance is less than value.
Zero 這個執行個體等於 valueThis instance is equal to value.
大於零Greater than zero 這個執行個體大於 valueThis instance is greater than value. -或--or- valuenullvalue is null.

實作

例外狀況

備註

任何執行個體Int32,不論其值會被視為大於nullAny instance of Int32, regardless of its value, is considered greater than null.

value 必須是null或執行個體Int32,否則會擲回例外狀況。value must be null or an instance of Int32; otherwise, an exception is thrown.

另請參閱

適用於