# Int32.CompareTo 方法

## 多載

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

## CompareTo(Int32)

``````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

#### 傳回

Zero 這個執行個體等於 `value`This instance is equal to `value`.

### 範例

``````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).
``````

## CompareTo(Object)

``````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

#### 傳回

Zero 這個執行個體等於 `value`This instance is equal to `value`.

#### 例外狀況

`value` 不是 Int32`value` is not an Int32.

### 備註

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