Int32.CompareTo 方法

定義

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

多載

CompareTo(Int32)

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

CompareTo(Object)

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

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

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

傳回

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

傳回值Return Value 描述Description
小於零 (0)Less than zero 這個執行個體小於 valueThis instance is less than value.
Zero 這個執行個體等於 valueThis instance is equal to value.
大於零 (0)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)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。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

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

傳回

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

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

實作

例外狀況

value 不是 Int32value is not an Int32.

備註

任何 Int32的實例(不論其值為何)都會視為大於 nullAny instance of Int32, regardless of its value, is considered greater than null.

value 必須 nullInt32的實例。否則,就會擲回例外狀況。value must be null or an instance of Int32; otherwise, an exception is thrown.

另請參閱

適用於