BigInteger.CompareTo Methode

Definitie

Vergelijkt de waarde van dit exemplaar met een andere waarde en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de andere waarde.

Overloads

Name Description
CompareTo(Int64)

Vergelijkt dit exemplaar met een ondertekend 64-bits geheel getal en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het ondertekende 64-bits geheel getal.

CompareTo(BigInteger)

Vergelijkt dit exemplaar met een seconde BigInteger en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het opgegeven object.

CompareTo(Object)

Vergelijkt dit exemplaar met een opgegeven object en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het opgegeven object.

CompareTo(UInt64)

Vergelijkt dit exemplaar met een niet-ondertekend 64-bits geheel getal en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het niet-ondertekende 64-bits gehele getal.

CompareTo(Int64)

Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs

Vergelijkt dit exemplaar met een ondertekend 64-bits geheel getal en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het ondertekende 64-bits geheel getal.

public:
 int CompareTo(long other);
public int CompareTo(long other);
member this.CompareTo : int64 -> int
Public Function CompareTo (other As Long) As Integer

Parameters

other
Int64

Het ondertekende 64-bits geheel getal dat moet worden vergeleken.

Retouren

Een ondertekende geheel getalwaarde die de relatie van dit exemplaar aangeeft other, zoals wordt weergegeven in de volgende tabel.

Retourwaarde Beschrijving
Kleiner dan nul Het huidige exemplaar is kleiner dan other.
Nul De huidige instantie is othergelijk aan .
Groter dan nul Het huidige exemplaar is groter dan other.

Voorbeelden

In het volgende voorbeeld ziet u het resultaat van het aanroepen van de CompareTo(Int64) methode met integrale waarden.

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
let bigIntValue = BigInteger.Parse "3221123045552"

let byteValue = 16uy
let sbyteValue = -16y
let shortValue = 1233s
let ushortValue = 1233us
let intValue = -12233
let uintValue = 12233u
let longValue = 12382222L
let ulongValue = 1238222UL

printfn $"Comparing {bigIntValue} with {byteValue}: {bigIntValue.CompareTo byteValue}"
printfn $"Comparing {bigIntValue} with {sbyteValue}: {bigIntValue.CompareTo sbyteValue}"
printfn $"Comparing {bigIntValue} with {shortValue}: {bigIntValue.CompareTo shortValue}"
printfn $"Comparing {bigIntValue} with {ushortValue}: {bigIntValue.CompareTo ushortValue}"
printfn $"Comparing {bigIntValue} with {intValue}: {bigIntValue.CompareTo intValue}"
printfn $"Comparing {bigIntValue} with {uintValue}: {bigIntValue.CompareTo uintValue}"
printfn $"Comparing {bigIntValue} with {longValue}: {bigIntValue.CompareTo longValue}"
printfn $"Comparing {bigIntValue} with {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
Dim bigIntValue As BigInteger = BigInteger.Parse("3221123045552")

Dim byteValue As Byte = 16
Dim sbyteValue As SByte = -16
Dim shortValue As Short = 1233      
Dim ushortValue As UShort = 1233
Dim intValue As Integer = -12233
Dim uintValue As UInteger = 12233
Dim longValue As Long = 12382222
Dim ulongValue As Integer = 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

Opmerkingen

Als other dit een Byte, Int16, Int32, of SByteUInt16UInt32 waarde is, wordt deze impliciet geconverteerd naar een Int64 waarde wanneer de CompareTo(Int64) methode wordt aangeroepen.

Van toepassing op

CompareTo(BigInteger)

Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs

Vergelijkt dit exemplaar met een seconde BigInteger en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het opgegeven object.

public:
 virtual int CompareTo(System::Numerics::BigInteger other);
public int CompareTo(System.Numerics.BigInteger other);
abstract member CompareTo : System.Numerics.BigInteger -> int
override this.CompareTo : System.Numerics.BigInteger -> int
Public Function CompareTo (other As BigInteger) As Integer

Parameters

other
BigInteger

Het object dat moet worden vergeleken.

Retouren

Een ondertekende geheel getalwaarde die de relatie van dit exemplaar aangeeft other, zoals wordt weergegeven in de volgende tabel.

Retourwaarde Beschrijving
Kleiner dan nul Het huidige exemplaar is kleiner dan other.
Nul De huidige instantie is othergelijk aan .
Groter dan nul Het huidige exemplaar is groter dan other.

Implementeringen

Voorbeelden

In het volgende voorbeeld ziet u het gebruik van de CompareTo(BigInteger) methode om een lijst StarInfo met objecten te ordenen. Elk StarInfo object bevat informatie over de naam van een ster en de afstand van de aarde in mijlen. StarInfo implementeert de IComparable<T> interface, waarmee StarInfo objecten kunnen worden gesorteerd op algemene verzamelingsklassen. De IComparable<T>.CompareTo implementatie verpakt gewoon een aanroep naar CompareTo(BigInteger).

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);
   }
}
open System
open System.Numerics

[<Struct; CustomComparison; StructuralEquality>]
type StarInfo =
    // Define fields
    val Name: string
    val Distance: BigInteger

    // Define constructors.
    new(name, lightYears) =
        { Name = name
          // Calculate distance in miles from light years.
          Distance = lightYears * 5.88e12 |> bigint }

    new(name, distance) = { Name = name; Distance = distance }

    // Display name of star and its distance in parentheses.
    override this.ToString() =
        $"{this.Name, -10} ({this.Distance:N0})"

    interface IComparable<StarInfo> with
        // Compare StarInfo objects by their distance from Earth.
        member this.CompareTo(other: StarInfo) = this.Distance.CompareTo(other.Distance)
Imports System.Collections.Generic
Imports System.Numerics

Public Structure StarInfo : Implements IComparable(Of StarInfo)
   ' Define constructors.
   Public Sub New(name As String, lightYears As Double)
      Me.Name = name
      ' Calculate distance in miles from light years.
      Me.Distance = CType(Math.Round(lightYears * 5.88e12), BigInteger)
   End Sub
   
   Public Sub New(name As String, distance As BigInteger)
      Me.Name = name
      Me.Distance = distance
   End Sub
   
   ' Define public fields.
   Public Name As String
   Public Distance As BigInteger

   ' Display name of star and its distance in parentheses.
   Public Overrides Function ToString() As String
      Return String.Format("{0,-10} ({1:N0})", Me.Name, Me.Distance)
   End Function

   ' Compare StarInfo objects by their distance from Earth.
   Public Function CompareTo(other As starInfo) As Integer _
                   Implements IComparable(Of StarInfo).CompareTo
      Return Me.Distance.CompareTo(other.Distance)
   End Function                
End Structure

Met de volgende code worden vervolgens vier StarInfo objecten geïnstitueert en opgeslagen in een algemeen List<T> object. Nadat de List<T>.Sort methode is aangeroepen, StarInfo worden objecten weergegeven in volgorde van hun afstand tot de aarde.

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)
let stars = ResizeArray()

let star1 = StarInfo("Sirius", 8.6)
stars.Add(star1)
let star2 = StarInfo("Rigel", 1400.)
stars.Add(star2)
let star3 = StarInfo("Castor", 49.)
stars.Add(star3)
let star4 = StarInfo("Antares", 520.)
stars.Add(star4)

stars.Sort()

for star in stars do
    printfn $"{star}"

// 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)
Module Example
   Public Sub Main()
      Dim star As StarInfo
      Dim stars As New List(Of 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()
      
      For Each star In stars
         Console.WriteLine(star)
      Next   
   End Sub
End Module
' 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)

Opmerkingen

Deze overbelasting van de CompareTo methode implementeert de IComparable<T>.CompareTo methode. Het wordt gebruikt door algemene verzamelingsobjecten om de items in de verzameling te orden.

Zie ook

Van toepassing op

CompareTo(Object)

Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs

Vergelijkt dit exemplaar met een opgegeven object en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het opgegeven object.

public:
 virtual int CompareTo(System::Object ^ obj);
public int CompareTo(object? obj);
public int CompareTo(object obj);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
Public Function CompareTo (obj As Object) As Integer

Parameters

obj
Object

Het object dat moet worden vergeleken.

Retouren

Een ondertekend geheel getal dat de relatie van het huidige exemplaar aan de obj parameter aangeeft, zoals wordt weergegeven in de volgende tabel.

Retourwaarde Beschrijving
Kleiner dan nul Het huidige exemplaar is kleiner dan obj.
Nul De huidige instantie is objgelijk aan .
Groter dan nul Het huidige exemplaar is groter dan obj, of de obj parameter is null.

Implementeringen

Uitzonderingen

Voorbeelden

In het volgende voorbeeld wordt de CompareTo(Object) methode aangeroepen om een BigInteger waarde te vergelijken met elk element in een objectmatrix:

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
let values =
    [| BigInteger.Pow(Int64.MaxValue, 10)
       Unchecked.defaultof<bigint>
       bigint 12.534
       Int64.MaxValue
       BigInteger.One |]

let number = bigint UInt64.MaxValue

for value in values do
    try
        printfn $"Comparing {number} with '{value}': {number.CompareTo value}"
    with :? ArgumentException as e ->
        printfn $"Unable to compare the {value.GetType().Name} value {value} with a BigInteger."
// 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

Opmerkingen

Deze overbelasting van de CompareTo methode implementeert de IComparable.CompareTo methode. Het wordt gebruikt door niet-algemene verzamelingsobjecten om de items in de verzameling te orden.

De obj parameter moet een van de volgende zijn:

  • Een object waarvan het uitvoeringstype is BigInteger.

  • Een Object variabele waarvan de waarde is null. Als de waarde van de obj parameter is null, retourneert de methode 1, wat aangeeft dat het huidige exemplaar groter is dan obj.

Zie ook

Van toepassing op

CompareTo(UInt64)

Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs
Bron:
BigInteger.cs

Belangrijk

Deze API is niet CLS-conform.

Vergelijkt dit exemplaar met een niet-ondertekend 64-bits geheel getal en retourneert een geheel getal dat aangeeft of de waarde van dit exemplaar kleiner is dan, gelijk is aan of groter is dan de waarde van het niet-ondertekende 64-bits gehele getal.

public:
 int CompareTo(System::UInt64 other);
[System.CLSCompliant(false)]
public int CompareTo(ulong other);
[<System.CLSCompliant(false)>]
member this.CompareTo : uint64 -> int
Public Function CompareTo (other As ULong) As Integer

Parameters

other
UInt64

Het niet-ondertekende 64-bits gehele getal dat moet worden vergeleken.

Retouren

Een ondertekend geheel getal dat de relatieve waarde van dit exemplaar aangeeft en other, zoals wordt weergegeven in de volgende tabel.

RetourwaardeBeschrijving
Kleiner dan nulHet huidige exemplaar is kleiner dan other.
NulDe huidige instantie is othergelijk aan .
Groter dan nulHet huidige exemplaar is groter dan other.

Kenmerken

Van toepassing op