BigInteger.IComparable.CompareTo(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
比較這個執行個體與特定物件,並且傳回一個整數,指出這個執行個體的值是小於、等於或大於特定物件的值。
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
參數
- obj
- Object
要比較的物件。
傳回
帶正負號的整數,表示目前執行個體與 obj
參數的關聯性,如下表所示。
傳回值 | 描述 |
---|---|
小於零 | 目前的執行個體小於 obj 。 |
零 | 目前的執行個體等於 obj 。 |
大於零 | 目前執行個體大於 obj ,或者 obj 參數為 null 。 |
實作
例外狀況
obj
不是 BigInteger。
範例
下列範例會 CompareTo(Object) 呼叫 方法,以比較 BigInteger 值與物件陣列中的每個元素:
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
備註
參數 obj
必須是下列其中一項:
運行時間類型為 BigInteger的物件。
值為 Object 的
null
變數。 如果 參數的值obj
是null
,則方法會傳回 1,表示目前的實例大於obj
。