BigInteger.CompareTo Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

Syntax

'Declaration
Public Function CompareTo ( _
    obj As Object _
) As Integer
public int CompareTo(
    Object obj
)

Parameters

Return Value

Type: System.Int32
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

The current instance is less than obj.

Zero

The current instance equals obj.

Greater than zero

The current instance is greater than obj, or the obj parameter is nulla null reference (Nothing in Visual Basic).

Implements

IComparable.CompareTo(Object)

Exceptions

Exception Condition
ArgumentException

obj is not a BigInteger.

Remarks

This overload of the CompareTo method implements the IComparable.CompareTo method. It is used by non-generic collection objects to order the items in the collection.

The obj parameter must be one of the following:

  • An object whose run-time type is BigInteger.

  • An Object variable whose value is nulla null reference (Nothing in Visual Basic). If the value of the obj parameter is nulla null reference (Nothing in Visual Basic), the method returns 1, which indicates that that the current instance is greater than obj.

Examples

The following example calls the CompareTo(Object) method to compare a BigInteger value with each element in an object array

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
      outputBlock.Text += String.Format("Comparing {0} with '{1}': {2}", number, value,  
                        number.CompareTo(value)) + vbCrLf
   Catch e As ArgumentException
      outputBlock.Text += String.Format("Unable to compare the {0} value {1} with a BigInteger.", 
                        value.GetType().Name, value) + vbCrLf
   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
object[] values = { BigInteger.Pow(Int64.MaxValue, 10), null, 
                    12.534, Int64.MaxValue, BigInteger.One };
BigInteger number = UInt64.MaxValue;

foreach (object value in values)
{
   try
   {
      outputBlock.Text += String.Format("Comparing {0} with '{1}': {2}\n", number, value,
                        number.CompareTo(value));
   }
   catch (ArgumentException)
   {
      outputBlock.Text += String.Format("Unable to compare the {0} value {1} with a BigInteger.\n",
                        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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.