BigInteger.CompareTo Metodo

Definizione

Confronta il valore di questa istanza con un altro valore e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore dell'altro valore.

Overload

CompareTo(Int64)

Confronta questa istanza con un intero con segno a 64 bit e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore del valore dell'intero con segno a 64 bit.

CompareTo(BigInteger)

Confronta questa istanza con un secondo BigInteger e restituisce un Integer che indica se il valore di questa istanza è minore, uguale o maggiore rispetto al valore dell'oggetto specificato.

CompareTo(Object)

Confronta questa istanza con un oggetto specificato e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore rispetto al valore dell'oggetto specificato.

CompareTo(UInt64)

Confronta questa istanza con un intero senza segno a 64 bit e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore del valore dell'intero senza segno a 64 bit.

CompareTo(Int64)

Origine:
BigInteger.cs
Origine:
BigInteger.cs
Origine:
BigInteger.cs

Confronta questa istanza con un intero con segno a 64 bit e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore del valore dell'intero con segno a 64 bit.

C#
public int CompareTo(long other);

Parametri

other
Int64

Intero con segno a 64 bit da confrontare.

Restituisce

Signed Integer che indica la relazione dell'istanza con other, come illustrato nella tabella seguente.

Valore restituito Descrizione
Minore di zero L'istanza corrente è minore di other.
Zero L'istanza corrente è uguale a other.
Maggiore di zero L'istanza corrente è maggiore di other.

Esempio

Nell'esempio seguente viene illustrato il risultato della chiamata al CompareTo(Int64) metodo con valori integrali.

C#
BigInteger bigIntValue = BigInteger.Parse("3221123045552");

byte byteValue = 16;
sbyte sbyteValue = -16;
short shortValue = 1233;
ushort ushortValue = 1233;
int intValue = -12233;
uint uintValue = 12233;
long longValue = 12382222;
ulong ulongValue = 1238222;

Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, byteValue,
                  bigIntValue.CompareTo(byteValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, sbyteValue,
                  bigIntValue.CompareTo(sbyteValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, shortValue,
                  bigIntValue.CompareTo(shortValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, ushortValue,
                  bigIntValue.CompareTo(ushortValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, intValue,
                  bigIntValue.CompareTo(intValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, uintValue,
                  bigIntValue.CompareTo(uintValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, longValue,
                  bigIntValue.CompareTo(longValue));
Console.WriteLine("Comparing {0} with {1}: {2}",
                  bigIntValue, ulongValue,
                  bigIntValue.CompareTo(ulongValue));
// The example displays the following output:
//       Comparing 3221123045552 with 16: 1
//       Comparing 3221123045552 with -16: 1
//       Comparing 3221123045552 with 1233: 1
//       Comparing 3221123045552 with 1233: 1
//       Comparing 3221123045552 with -12233: 1
//       Comparing 3221123045552 with 12233: 1
//       Comparing 3221123045552 with 12382222: 1
//       Comparing 3221123045552 with 1238222: 1

Commenti

Se other è un Bytevalore , Int32Int16UInt16SByteo o UInt32 viene convertito in modo implicito in un Int64 valore quando viene chiamato il CompareTo(Int64) metodo.

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CompareTo(BigInteger)

Origine:
BigInteger.cs
Origine:
BigInteger.cs
Origine:
BigInteger.cs

Confronta questa istanza con un secondo BigInteger e restituisce un Integer che indica se il valore di questa istanza è minore, uguale o maggiore rispetto al valore dell'oggetto specificato.

C#
public int CompareTo(System.Numerics.BigInteger other);

Parametri

other
BigInteger

Oggetto da confrontare.

Restituisce

Signed Integer che indica la relazione dell'istanza con other, come illustrato nella tabella seguente.

Valore restituito Descrizione
Minore di zero L'istanza corrente è minore di other.
Zero L'istanza corrente è uguale a other.
Maggiore di zero L'istanza corrente è maggiore di other.

Implementazioni

Esempio

Nell'esempio seguente viene illustrato l'uso CompareTo(BigInteger) del metodo per ordinare un elenco di StarInfo oggetti. Ogni StarInfo oggetto fornisce informazioni sul nome di un star e sulla sua distanza dalla Terra in miglia. StarInfo implementa l'interfaccia IComparable<T> , che consente StarInfo di ordinare gli oggetti in base a classi di raccolta generiche. L'implementazione IComparable<T>.CompareTo esegue semplicemente il wrapping di una chiamata a CompareTo(BigInteger).

C#
using System;
using System.Collections.Generic;
using System.Numerics;

public struct StarInfo : IComparable<StarInfo>
{
   // Define constructors.
   public StarInfo(string name, double lightYears)
   {
      this.Name = name;

      // Calculate distance in miles from light years.
      this.Distance = (BigInteger) Math.Round(lightYears * 5.88e12);
   }

   public StarInfo(string name, BigInteger distance)
   {
      this.Name = name;
      this.Distance = distance;
   }

   // Define public fields.
   public string Name;
   public BigInteger Distance;

   // Display name of star and its distance in parentheses.
   public override string ToString()
   {
      return String.Format("{0,-10} ({1:N0})", this.Name, this.Distance);
   }

   // Compare StarInfo objects by their distance from Earth.
   public int CompareTo(StarInfo other)
   {
      return this.Distance.CompareTo(other.Distance);
   }
}

Il codice seguente crea quindi un'istanza di quattro StarInfo oggetti e li archivia in un oggetto generico List<T> . Dopo aver chiamato il List<T>.Sort metodo, StarInfo gli oggetti vengono visualizzati in ordine di distanza dalla Terra.

C#
public class Example
{
   public static void Main()
   {
      StarInfo star;
      List<StarInfo> stars = new List<StarInfo>();

      star = new StarInfo("Sirius", 8.6d);
      stars.Add(star);
      star = new StarInfo("Rigel", 1400d);
      stars.Add(star);
      star = new StarInfo("Castor", 49d);
      stars.Add(star);
      star = new StarInfo("Antares", 520d);
      stars.Add(star);

      stars.Sort();

      foreach (StarInfo sortedStar in stars)
         Console.WriteLine(sortedStar);
   }
}
// The example displays the following output:
//       Sirius     (50,568,000,000,000)
//       Castor     (288,120,000,000,000)
//       Antares    (3,057,600,000,000,000)
//       Rigel      (8,232,000,000,000,000)

Commenti

Questo overload del CompareTo metodo implementa il IComparable<T>.CompareTo metodo . Viene usato dagli oggetti di raccolta generici per ordinare gli elementi nella raccolta.

Vedi anche

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CompareTo(Object)

Origine:
BigInteger.cs
Origine:
BigInteger.cs
Origine:
BigInteger.cs

Confronta questa istanza con un oggetto specificato e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore rispetto al valore dell'oggetto specificato.

C#
public int CompareTo(object? obj);
C#
public int CompareTo(object obj);

Parametri

obj
Object

Oggetto da confrontare.

Restituisce

Signed Integer che indica la relazione dell'istanza corrente con il parametro obj, come illustrato nella tabella seguente.

Valore restituito Descrizione
Minore di zero L'istanza corrente è minore di obj.
Zero L'istanza corrente è uguale a obj.
Maggiore di zero L'istanza corrente è maggiore di obj, oppure il parametro obj è null.

Implementazioni

Eccezioni

obj non è un oggetto BigInteger.

Esempio

Nell'esempio seguente viene chiamato il CompareTo(Object) metodo per confrontare un BigInteger valore con ogni elemento in una matrice di oggetti:

C#
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

Commenti

Questo overload del CompareTo metodo implementa il IComparable.CompareTo metodo . Viene usato dagli oggetti raccolta non generici per ordinare gli elementi dell'insieme.

Il obj parametro deve essere uno dei seguenti:

  • Oggetto il cui tipo di runtime è BigInteger.

  • Variabile Object il cui valore è null. Se il valore del obj parametro è null, il metodo restituisce 1, che indica che l'istanza corrente è maggiore di obj.

Vedi anche

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CompareTo(UInt64)

Origine:
BigInteger.cs
Origine:
BigInteger.cs
Origine:
BigInteger.cs

Importante

Questa API non è conforme a CLS.

Confronta questa istanza con un intero senza segno a 64 bit e restituisce un intero che indica se il valore di questa istanza è minore, uguale o maggiore del valore dell'intero senza segno a 64 bit.

C#
[System.CLSCompliant(false)]
public int CompareTo(ulong other);

Parametri

other
UInt64

Intero senza segno a 64 bit da confrontare.

Restituisce

Signed Integer che indica il valore relativo dell'istanza e other, come illustrato nella tabella seguente.

Valore restituitoDescrizione
Minore di zeroL'istanza corrente è minore di other.
ZeroL'istanza corrente è uguale a other.
Maggiore di zeroL'istanza corrente è maggiore di other.

Attributi

Si applica a

.NET 10 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0