BigInteger.IComparable.CompareTo(Object) Método

Definición

Compara esta instancia con un objeto especificado y devuelve un entero que indica si el valor de esta instancia es mayor, igual o menor que el valor del objeto especificado.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);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo

Parámetros

obj
Object

Objeto que se va a comparar.The object to compare.

Devoluciones

Entero con signo que indica la relación de la instancia actual con el parámetro obj, como se muestra en la tabla siguiente.A signed integer that indicates the relationship of the current instance to the obj parameter, as shown in the following table.

Valor devueltoReturn value DescripciónDescription
Menor que ceroLess than zero La instancia actual es menor que obj.The current instance is less than obj.
CeroZero La instancia actual es igual que obj.The current instance equals obj.
Mayor que ceroGreater than zero La instancia actual es mayor que obj o el parámetro obj es null.The current instance is greater than obj, or the obj parameter is null.

Implementaciones

Excepciones

obj no es un BigInteger.obj is not a BigInteger.

Ejemplos

En el ejemplo siguiente se llama al método CompareTo(Object) para comparar un valor de BigInteger con cada elemento de una matriz de objetos: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

Comentarios

El parámetro obj debe ser uno de los siguientes:The obj parameter must be one of the following:

  • Objeto cuyo tipo en tiempo de ejecución es BigInteger.An object whose run-time type is BigInteger.

  • Una variable de Object cuyo valor es null.An Object variable whose value is null. Si el valor del parámetro obj es null, el método devuelve 1, lo que indica que la instancia actual es mayor que obj.If the value of the obj parameter is null, the method returns 1, which indicates that the current instance is greater than obj.

Se aplica a

Consulte también: