BigInteger.CompareTo Yöntem

Tanım

Bu örneğin değerini başka bir değerle karşılaştırır ve bu örneğin değerinin diğer değerden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

Aşırı Yüklemeler

CompareTo(Int64)

Bu örneği imzalı bir 64 bit tamsayıyla karşılaştırır ve bu örneğin değerinin imzalı 64 bit tamsayı değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

CompareTo(BigInteger)

Bu örneği bir saniyeyle BigInteger karşılaştırır ve bu örneğin değerinin belirtilen nesnenin değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

CompareTo(Object)

Bu örneği belirtilen bir nesneyle karşılaştırır ve bu örneğin değerinin belirtilen nesnenin değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

CompareTo(UInt64)

Bu örneği işaretsiz bir 64 bit tamsayıyla karşılaştırır ve bu örneğin değerinin işaretsiz 64 bit tamsayı değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

CompareTo(Int64)

Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs

Bu örneği imzalı bir 64 bit tamsayıyla karşılaştırır ve bu örneğin değerinin imzalı 64 bit tamsayı değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

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

Parametreler

other
Int64

Karşılaştıracak imzalı 64 bit tamsayı.

Döndürülenler

Aşağıdaki tabloda gösterildiği gibi, bu örneğin otherile ilişkisini gösteren imzalı bir tamsayı değeri.

Döndürülen değer Açıklama
Sıfırdan küçük Geçerli örnek değerinden otherküçük.
Sıfır Geçerli örnek eşittir other.
Sıfırdan büyük Geçerli örnek değerinden otherbüyüktür.

Örnekler

Aşağıdaki örnekte, yöntemini tam sayı değerleriyle çağırmanın CompareTo(Int64) sonucu gösterilmektedir.

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

Açıklamalar

bir , Int16, , Int32, SByteUInt16veya UInt32 değeriyseother, yöntem çağrıldığında CompareTo(Int64) örtük olarak bir Int64 değere Bytedönüştürülür.

Şunlara uygulanır

CompareTo(BigInteger)

Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs

Bu örneği bir saniyeyle BigInteger karşılaştırır ve bu örneğin değerinin belirtilen nesnenin değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

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

Parametreler

other
BigInteger

Karşılaştırma yapılacak nesne.

Döndürülenler

Aşağıdaki tabloda gösterildiği gibi, bu örneğin otherile ilişkisini gösteren imzalı bir tamsayı değeri.

Döndürülen değer Açıklama
Sıfırdan küçük Geçerli örnek değerinden otherküçük.
Sıfır Geçerli örnek eşittir other.
Sıfırdan büyük Geçerli örnek değerinden otherbüyüktür.

Uygulamalar

Örnekler

Aşağıdaki örnekte, nesne listesini sıralamak için yönteminin CompareTo(BigInteger) kullanımı gösterilmektedir StarInfo . Her StarInfo nesne, bir star adı ve mil olarak Dünya'dan uzaklığı hakkında bilgi sağlar. StarInfoIComparable<T>, nesnelerin genel koleksiyon sınıflarına göre sıralanmasını sağlayan StarInfo arabirimini uygular. Uygulaması IComparable<T>.CompareTo yalnızca çağrısına CompareTo(BigInteger)sarmalar.

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);
   }
}
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

Aşağıdaki kod daha sonra dört StarInfo nesnenin örneğini oluşturur ve bunları genel List<T> bir nesnede depolar. List<T>.Sort Yöntem çağrıldıktan sonra nesneler Dünya'dan StarInfo uzaklıkları sırasına göre görüntülenir.

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)
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)

Açıklamalar

Yönteminin CompareTo bu aşırı yüklemesi yöntemini uygular IComparable<T>.CompareTo . Genel koleksiyon nesneleri tarafından koleksiyondaki öğeleri sıralamak için kullanılır.

Ayrıca bkz.

Şunlara uygulanır

CompareTo(Object)

Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs

Bu örneği belirtilen bir nesneyle karşılaştırır ve bu örneğin değerinin belirtilen nesnenin değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

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

Parametreler

obj
Object

Karşılaştırma yapılacak nesne.

Döndürülenler

Aşağıdaki tabloda gösterildiği gibi geçerli örneğin parametresiyle obj ilişkisini gösteren imzalı bir tamsayı.

Döndürülen değer Açıklama
Sıfırdan küçük Geçerli örnek değerinden objküçük.
Sıfır Geçerli örnek eşittir obj.
Sıfırdan büyük Geçerli örnek değerinden objobj büyük veya parametresidirnull.

Uygulamalar

Özel durumlar

obj bir BigIntegerdeğildir.

Örnekler

Aşağıdaki örnek, bir değeri bir BigInteger nesne dizisindeki her öğeyle karşılaştırmak için yöntemini çağırırCompareTo(Object):

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

Açıklamalar

Yönteminin CompareTo bu aşırı yüklemesi yöntemini uygular IComparable.CompareTo . Genel olmayan koleksiyon nesneleri tarafından koleksiyondaki öğeleri sıralamak için kullanılır.

obj parametresi aşağıdakilerden biri olmalıdır:

  • Çalışma zamanı türü olan bir nesne.BigInteger

  • Object Değeri olan bir değişken.null parametresinin obj değeri ise null, yöntemi geçerli örneğin değerinden büyük objolduğunu belirten 1 döndürür.

Ayrıca bkz.

Şunlara uygulanır

CompareTo(UInt64)

Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs
Kaynak:
BigInteger.cs

Önemli

Bu API, CLS uyumlu değildir.

Bu örneği işaretsiz bir 64 bit tamsayıyla karşılaştırır ve bu örneğin değerinin işaretsiz 64 bit tamsayı değerinden küçük, buna eşit veya ondan büyük olup olmadığını belirten bir tamsayı döndürür.

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

Parametreler

other
UInt64

Karşılaştırılan işaretsiz 64 bit tamsayı.

Döndürülenler

Aşağıdaki tabloda gösterildiği gibi, bu örneğin ve otheröğesinin göreli değerini gösteren imzalı bir tamsayı.

Döndürülen değerAçıklama
Sıfırdan küçükGeçerli örnek değerinden otherküçük.
SıfırGeçerli örnek eşittir other.
Sıfırdan büyükGeçerli örnek değerinden otherbüyüktür.
Öznitelikler

Şunlara uygulanır