BigInteger.Equals 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個表示兩個數值是否相等的值。
多載
| 名稱 | Description |
|---|---|
| Equals(UInt64) |
回傳一個值,表示目前實例與無號 64 位元整數是否具有相同值。 |
| Equals(Object) |
回傳一個值,表示目前實例與指定物件的值是否相同。 |
| Equals(BigInteger) |
回傳一個值,表示目前實例與指定 BigInteger 物件的值是否相同。 |
| Equals(Int64) |
回傳一個值,表示目前實例與有符號的 64 位元整數值是否相同。 |
Equals(UInt64)
重要
此 API 不符合 CLS 規範。
回傳一個值,表示目前實例與無號 64 位元整數是否具有相同值。
public:
bool Equals(System::UInt64 other);
[System.CLSCompliant(false)]
public bool Equals(ulong other);
[<System.CLSCompliant(false)>]
override this.Equals : uint64 -> bool
Public Function Equals (other As ULong) As Boolean
參數
- other
- UInt64
用來比較的無符號 64 位元整數。
傳回
true如果目前實例與無符號的 64 位元整數值相同;否則,。 false
- 屬性
範例
以下範例比較了幾顆恆星與地球的近似距離與 Epsilon Indi 距離地球的距離,以判斷它們是否相等。 範例利用方法的 Equals 每次超載來檢驗相等性。
const long LIGHT_YEAR = 5878625373183;
BigInteger altairDistance = 17 * LIGHT_YEAR;
BigInteger epsilonIndiDistance = 12 * LIGHT_YEAR;
BigInteger ursaeMajoris47Distance = 46 * LIGHT_YEAR;
long tauCetiDistance = 12 * LIGHT_YEAR;
ulong procyon2Distance = 12 * LIGHT_YEAR;
object wolf424ABDistance = 14 * LIGHT_YEAR;
Console.WriteLine("Approx. equal distances from Epsilon Indi to:");
Console.WriteLine(" Altair: {0}",
epsilonIndiDistance.Equals(altairDistance));
Console.WriteLine(" Ursae Majoris 47: {0}",
epsilonIndiDistance.Equals(ursaeMajoris47Distance));
Console.WriteLine(" TauCeti: {0}",
epsilonIndiDistance.Equals(tauCetiDistance));
Console.WriteLine(" Procyon 2: {0}",
epsilonIndiDistance.Equals(procyon2Distance));
Console.WriteLine(" Wolf 424 AB: {0}",
epsilonIndiDistance.Equals(wolf424ABDistance));
// The example displays the following output:
// Approx. equal distances from Epsilon Indi to:
// Altair: False
// Ursae Majoris 47: False
// TauCeti: True
// Procyon 2: True
// Wolf 424 AB: False
let LIGHT_YEAR = 5878625373183L
let altairDistance = 17I * bigint LIGHT_YEAR
let epsilonIndiDistance = 12I * bigint LIGHT_YEAR
let ursaeMajoris47Distance = 46I * bigint LIGHT_YEAR
let tauCetiDistance = 12L * LIGHT_YEAR
let procyon2Distance = 12uL * uint64 LIGHT_YEAR
let wolf424ABDistance = 14L * LIGHT_YEAR
printfn "Approx. equal distances from Epsilon Indi to:"
printfn $" Altair: {epsilonIndiDistance.Equals(altairDistance)}"
printfn $" Ursae Majoris 47: {epsilonIndiDistance.Equals(ursaeMajoris47Distance)}"
printfn $" TauCeti: {epsilonIndiDistance.Equals(tauCetiDistance)}"
printfn $" Procyon 2: {epsilonIndiDistance.Equals(procyon2Distance)}"
printfn $" Wolf 424 AB: {epsilonIndiDistance.Equals(wolf424ABDistance)}"
// The example displays the following output:
// Approx. equal distances from Epsilon Indi to:
// Altair: False
// Ursae Majoris 47: False
// TauCeti: True
// Procyon 2: True
// Wolf 424 AB: False
Const LIGHT_YEAR As Long = 5878625373183
Dim altairDistance As BigInteger = 17 * LIGHT_YEAR
Dim epsilonIndiDistance As BigInteger = 12 * LIGHT_YEAR
Dim ursaeMajoris47Distance As BigInteger = 46 * LIGHT_YEAR
Dim tauCetiDistance As BigInteger = 12 * LIGHT_YEAR
Dim procyon2Distance As Long = 12 * LIGHT_YEAR
Dim wolf424ABDistance As Object = 14 * LIGHT_YEAR
Console.WriteLine("Approx. equal distances from Epsilon Indi to:")
Console.WriteLine(" Altair: {0}", _
epsilonIndiDistance.Equals(altairDistance))
Console.WriteLine(" Ursae Majoris 47: {0}", _
epsilonIndiDistance.Equals(ursaeMajoris47Distance))
Console.WriteLine(" TauCeti: {0}", _
epsilonIndiDistance.Equals(tauCetiDistance))
Console.WriteLine(" Procyon 2: {0}", _
epsilonIndiDistance.Equals(procyon2Distance))
Console.WriteLine(" Wolf 424 AB: {0}", _
epsilonIndiDistance.Equals(wolf424ABDistance))
' The example displays the following output:
' Approx. equal distances from Epsilon Indi to:
' Altair: False
' Ursae Majoris 47: False
' TauCeti: True
' Procyon 2: True
' Wolf 424 AB: False
備註
為了判斷兩個物件之間的關係,而不僅僅是測試相等,請呼叫該 BigInteger.CompareTo(UInt64) 方法。
適用於
Equals(Object)
回傳一個值,表示目前實例與指定物件的值是否相同。
public:
override bool Equals(System::Object ^ obj);
public override bool Equals(object obj);
public override bool Equals(object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean
參數
- obj
- Object
要比較的物件。
傳回
true若參數obj為BigInteger物件,且其值等於當前BigInteger實例的值;否則,。 false
範例
以下範例定義了平行 Object 與 BigInteger 陣列。 同一陣列的每個元素值與第二個陣列對應元素相同。 如範例輸出所示,陣列中的BigInteger實例只有在後者為 a Object 且值相等時,才被視為等於陣列中的BigInteger實例。
using System;
using System.Numerics;
public class Example
{
public static void Main()
{
object[] obj = { 0, 10, 100, new BigInteger(1000), -10 };
BigInteger[] bi = { BigInteger.Zero, new BigInteger(10),
new BigInteger(100), new BigInteger(1000),
new BigInteger(-10) };
for (int ctr = 0; ctr < bi.Length; ctr++)
Console.WriteLine(bi[ctr].Equals(obj[ctr]));
}
}
// The example displays the following output:
// False
// False
// False
// True
// False
open System.Numerics
let obj: obj[] = [| 0; 10; 100; BigInteger 1000; -10 |]
let bi =
[| BigInteger.Zero
BigInteger 10
BigInteger 100
BigInteger 1000
BigInteger -10 |]
for ctr = 0 to bi.Length - 1 do
printfn $"{bi.[ctr].Equals(obj.[ctr])}"
// The example displays the following output:
// False
// False
// False
// True
// False
Imports System.Numerics
Module Example
Public Sub Main()
Dim obj() As object = { 0, 10, 100, New BigInteger(1000), -10 }
Dim bi() As BigInteger = { BigInteger.Zero, New BigInteger(10),
New BigInteger(100), New BigInteger(1000),
New BigInteger(-10) }
For ctr As Integer = 0 To bi.Length - 1
Console.WriteLine(bi(ctr).Equals(obj(ctr)))
Next
End Sub
End Module
' The example displays the following output:
' False
' False
' False
' True
' False
備註
若參數 obj 不是 BigInteger 值,則方法回傳 false。 該方法僅在true值obj等於當前實例時回傳BigInteger。
為了判斷兩個物件之間的關係,而不僅僅是測試相等,請呼叫該 CompareTo(Object) 方法。
適用於
Equals(BigInteger)
回傳一個值,表示目前實例與指定 BigInteger 物件的值是否相同。
public:
virtual bool Equals(System::Numerics::BigInteger other);
public bool Equals(System.Numerics.BigInteger other);
override this.Equals : System.Numerics.BigInteger -> bool
Public Function Equals (other As BigInteger) As Boolean
參數
- other
- BigInteger
要比較的物件。
傳回
true 若此 BigInteger 物件與 other 值相同;否則, false。
實作
範例
以下範例比較了幾顆恆星與地球的近似距離與 Epsilon Indi 距離地球的距離,以判斷它們是否相等。 範例利用方法的 Equals 每次超載來檢驗相等性。
const long LIGHT_YEAR = 5878625373183;
BigInteger altairDistance = 17 * LIGHT_YEAR;
BigInteger epsilonIndiDistance = 12 * LIGHT_YEAR;
BigInteger ursaeMajoris47Distance = 46 * LIGHT_YEAR;
long tauCetiDistance = 12 * LIGHT_YEAR;
ulong procyon2Distance = 12 * LIGHT_YEAR;
object wolf424ABDistance = 14 * LIGHT_YEAR;
Console.WriteLine("Approx. equal distances from Epsilon Indi to:");
Console.WriteLine(" Altair: {0}",
epsilonIndiDistance.Equals(altairDistance));
Console.WriteLine(" Ursae Majoris 47: {0}",
epsilonIndiDistance.Equals(ursaeMajoris47Distance));
Console.WriteLine(" TauCeti: {0}",
epsilonIndiDistance.Equals(tauCetiDistance));
Console.WriteLine(" Procyon 2: {0}",
epsilonIndiDistance.Equals(procyon2Distance));
Console.WriteLine(" Wolf 424 AB: {0}",
epsilonIndiDistance.Equals(wolf424ABDistance));
// The example displays the following output:
// Approx. equal distances from Epsilon Indi to:
// Altair: False
// Ursae Majoris 47: False
// TauCeti: True
// Procyon 2: True
// Wolf 424 AB: False
let LIGHT_YEAR = 5878625373183L
let altairDistance = 17I * bigint LIGHT_YEAR
let epsilonIndiDistance = 12I * bigint LIGHT_YEAR
let ursaeMajoris47Distance = 46I * bigint LIGHT_YEAR
let tauCetiDistance = 12L * LIGHT_YEAR
let procyon2Distance = 12uL * uint64 LIGHT_YEAR
let wolf424ABDistance = 14L * LIGHT_YEAR
printfn "Approx. equal distances from Epsilon Indi to:"
printfn $" Altair: {epsilonIndiDistance.Equals(altairDistance)}"
printfn $" Ursae Majoris 47: {epsilonIndiDistance.Equals(ursaeMajoris47Distance)}"
printfn $" TauCeti: {epsilonIndiDistance.Equals(tauCetiDistance)}"
printfn $" Procyon 2: {epsilonIndiDistance.Equals(procyon2Distance)}"
printfn $" Wolf 424 AB: {epsilonIndiDistance.Equals(wolf424ABDistance)}"
// The example displays the following output:
// Approx. equal distances from Epsilon Indi to:
// Altair: False
// Ursae Majoris 47: False
// TauCeti: True
// Procyon 2: True
// Wolf 424 AB: False
Const LIGHT_YEAR As Long = 5878625373183
Dim altairDistance As BigInteger = 17 * LIGHT_YEAR
Dim epsilonIndiDistance As BigInteger = 12 * LIGHT_YEAR
Dim ursaeMajoris47Distance As BigInteger = 46 * LIGHT_YEAR
Dim tauCetiDistance As BigInteger = 12 * LIGHT_YEAR
Dim procyon2Distance As Long = 12 * LIGHT_YEAR
Dim wolf424ABDistance As Object = 14 * LIGHT_YEAR
Console.WriteLine("Approx. equal distances from Epsilon Indi to:")
Console.WriteLine(" Altair: {0}", _
epsilonIndiDistance.Equals(altairDistance))
Console.WriteLine(" Ursae Majoris 47: {0}", _
epsilonIndiDistance.Equals(ursaeMajoris47Distance))
Console.WriteLine(" TauCeti: {0}", _
epsilonIndiDistance.Equals(tauCetiDistance))
Console.WriteLine(" Procyon 2: {0}", _
epsilonIndiDistance.Equals(procyon2Distance))
Console.WriteLine(" Wolf 424 AB: {0}", _
epsilonIndiDistance.Equals(wolf424ABDistance))
' The example displays the following output:
' Approx. equal distances from Epsilon Indi to:
' Altair: False
' Ursae Majoris 47: False
' TauCeti: True
' Procyon 2: True
' Wolf 424 AB: False
備註
此方法實 IEquatable<T> 作介面且表現略優 Equals(Object) 於 ,因為不必將參數轉換為 other 物件 BigInteger 。
為了判斷兩個 BigInteger 物件之間的關係,而不僅僅是測試相等,請呼叫該 BigInteger.CompareTo(BigInteger) 方法。
適用於
Equals(Int64)
回傳一個值,表示目前實例與有符號的 64 位元整數值是否相同。
public:
bool Equals(long other);
public bool Equals(long other);
override this.Equals : int64 -> bool
Public Function Equals (other As Long) As Boolean
參數
- other
- Int64
用有符號的 64 位元整數值比較。
傳回
true若有符號的 64 位元整數與當前實例值相同;否則,。 false
範例
以下範例實例化 BigInteger 每個整數類型的物件,唯獨 UInt64。 接著它呼叫Equals(Int64)該方法,將該值與傳給BigInteger建構子的原始整數值進行比較BigInteger。 如輸出所示,兩者在兩種情況下都是相等的。
BigInteger bigIntValue;
byte byteValue = 16;
bigIntValue = new BigInteger(byteValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
byteValue.GetType().Name, byteValue,
bigIntValue.Equals(byteValue));
sbyte sbyteValue = -16;
bigIntValue = new BigInteger(sbyteValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
sbyteValue.GetType().Name, sbyteValue,
bigIntValue.Equals(sbyteValue));
short shortValue = 1233;
bigIntValue = new BigInteger(shortValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
shortValue.GetType().Name, shortValue,
bigIntValue.Equals(shortValue));
ushort ushortValue = 64000;
bigIntValue = new BigInteger(ushortValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
ushortValue.GetType().Name, ushortValue,
bigIntValue.Equals(ushortValue));
int intValue = -1603854;
bigIntValue = new BigInteger(intValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
intValue.GetType().Name, intValue,
bigIntValue.Equals(intValue));
uint uintValue = 1223300;
bigIntValue = new BigInteger(uintValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
uintValue.GetType().Name, uintValue,
bigIntValue.Equals(uintValue));
long longValue = -123822229012;
bigIntValue = new BigInteger(longValue);
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
longValue.GetType().Name, longValue,
bigIntValue.Equals(longValue));
// The example displays the following output:
// BigInteger 16 = Byte 16 : True
// BigInteger -16 = SByte -16 : True
// BigInteger 1233 = Int16 1233 : True
// BigInteger 64000 = UInt16 64000 : True
// BigInteger -1603854 = Int32 -1603854 : True
// BigInteger 1223300 = UInt32 1223300 : True
// BigInteger -123822229012 = Int64 -123822229012 : True
let byteValue = 16uy
let bigIntValue = BigInteger byteValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {byteValue.GetType().Name} {byteValue} : {bigIntValue.Equals(byteValue)}"
let sbyteValue = -16y
let bigIntValue = BigInteger sbyteValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {sbyteValue.GetType().Name} {sbyteValue} : {bigIntValue.Equals(sbyteValue)}"
let shortValue = 1233s
let bigIntValue = BigInteger shortValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {shortValue.GetType().Name} {shortValue} : {bigIntValue.Equals(shortValue)}"
let ushortValue = 64000us
let bigIntValue = BigInteger ushortValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {ushortValue.GetType().Name} {ushortValue} : {bigIntValue.Equals(ushortValue)}"
let intValue = -1603854
let bigIntValue = BigInteger intValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {intValue.GetType().Name} {intValue} : {bigIntValue.Equals(intValue)}"
let uintValue = 1223300u
let bigIntValue = BigInteger uintValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {uintValue.GetType().Name} {uintValue} : {bigIntValue.Equals(uintValue)}"
let longValue = -123822229012L
let bigIntValue = BigInteger longValue
printfn
$"{bigIntValue.GetType().Name} {bigIntValue} = {longValue.GetType().Name} {longValue} : {bigIntValue.Equals(longValue)}"
// The example displays the following output:
// BigInteger 16 = Byte 16 : True
// BigInteger -16 = SByte -16 : True
// BigInteger 1233 = Int16 1233 : True
// BigInteger 64000 = UInt16 64000 : True
// BigInteger -1603854 = Int32 -1603854 : True
// BigInteger 1223300 = UInt32 1223300 : True
// BigInteger -123822229012 = Int64 -123822229012 : True
Dim bigIntValue As BigInteger
Dim byteValue As Byte = 16
bigIntValue = New BigInteger(byteValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
byteValue.GetType().Name, byteValue,
bigIntValue.Equals(byteValue))
Dim sbyteValue As SByte = -16
bigIntValue = New BigInteger(sbyteValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
sbyteValue.GetType().Name, sbyteValue,
bigIntValue.Equals(sbyteValue))
Dim shortValue As Short = 1233
bigIntValue = New BigInteger(shortValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
shortValue.GetType().Name, shortValue,
bigIntValue.Equals(shortValue))
Dim ushortValue As UShort = 64000
bigIntValue = New BigInteger(ushortValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
ushortValue.GetType().Name, ushortValue,
bigIntValue.Equals(ushortValue))
Dim intValue As Integer = -1603854
bigIntValue = New BigInteger(intValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
intValue.GetType().Name, intValue,
bigIntValue.Equals(intValue))
Dim uintValue As UInteger = 1223300
bigIntValue = New BigInteger(uintValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
uintValue.GetType().Name, uintValue,
bigIntValue.Equals(uintValue))
Dim longValue As Long = -123822229012
bigIntValue = New BigInteger(longValue)
Console.WriteLine("{0} {1} = {2} {3} : {4}",
bigIntValue.GetType().Name, bigIntValue,
longValue.GetType().Name, longValue,
bigIntValue.Equals(longValue))
' The example displays the following output:
' BigInteger 16 = Byte 16 : True
' BigInteger -16 = SByte -16 : True
' BigInteger 1233 = Int16 1233 : True
' BigInteger 64000 = UInt16 64000 : True
' BigInteger -1603854 = Int32 -1603854 : True
' BigInteger 1223300 = UInt32 1223300 : True
' BigInteger -123822229012 = Int64 -123822229012 : True
備註
若 other 是 Byte、 Int16、 Int32SByte、 UInt16、 或 UInt32 值,當呼叫該方法時,該值會隱含轉換為值Int64。
為了判斷兩個物件之間的關係,而不僅僅是測試相等,請呼叫該 BigInteger.CompareTo(Int64) 方法。