Int32.CompareTo 方法

重载

 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)

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

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

返回

Int32

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.