BigInteger.IComparable.CompareTo(Object) 方法

定义

将此实例与指定对象进行比较,并返回一个整数,该整数指示此实例的值是小于、等于还是大于指定对象的值。Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

 virtual int System.IComparable.CompareTo(System::Object ^ obj) = IComparable::CompareTo;
int IComparable.CompareTo (object obj);
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo

参数

obj
Object

要比较的对象。The object to compare.

返回

一个带符号整数,指示当前实例与 obj 参数的关系,如下表所示。A signed integer that indicates the relationship of the current instance to the obj parameter, as shown in the following table.

返回值Return value 说明Description
小于零Less than zero 当前实例小于 objThe current instance is less than obj.
Zero 当前实例等于 objThe current instance equals obj.
大于零Greater than zero 当前实例大于 obj,或者 obj 参数为 nullThe current instance is greater than obj, or the obj parameter is null.

实现

异常

obj 不是 BigIntegerobj is not a BigInteger.

示例

下面的示例调用 CompareTo(Object) 方法,将 BigInteger 值与对象数组中的每个元素进行比较:The following example calls the CompareTo(Object) method to compare a BigInteger value with each element in an object array:

object[] values = { BigInteger.Pow(Int64.MaxValue, 10), null, 
                    12.534, Int64.MaxValue, BigInteger.One };
BigInteger number = UInt64.MaxValue;

foreach (object value in values)
{
   try {
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, 
                        number.CompareTo(value));
   }
   catch (ArgumentException) {
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value);
   }
}                                 
// The example displays the following output:
//    Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1
//    Comparing 18446744073709551615 with '': 1
//    Unable to compare the Double value 12.534 with a BigInteger.
//    Unable to compare the Int64 value 9223372036854775807 with a BigInteger.
//    Comparing 18446744073709551615 with '1': 1
Dim values() As Object = { BigInteger.Pow(Int64.MaxValue, 10), Nothing, 
                           12.534, Int64.MaxValue, BigInteger.One }
Dim number As BigInteger = UInt64.MaxValue

For Each value As Object In values
   Try
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, 
                        number.CompareTo(value))
   Catch e As ArgumentException
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value)
   End Try                     
Next                                 
' The example displays the following output:
'    Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1
'    Comparing 18446744073709551615 with '': 1
'    Unable to compare the Double value 12.534 with a BigInteger.
'    Unable to compare the Int64 value 9223372036854775807 with a BigInteger.
'    Comparing 18446744073709551615 with '1': 1

注解

obj 参数必须为以下参数之一:The obj parameter must be one of the following:

  • BigInteger其运行时类型的对象。An object whose run-time type is BigInteger.

  • 值为 nullObject 变量。An Object variable whose value is null. 如果 null``obj 参数的值,则该方法将返回1,指示当前实例大于 objIf the value of the obj parameter is null, the method returns 1, which indicates that the current instance is greater than obj.

适用于

另请参阅