BigInteger.Equals Method

Definition

Returns a value that indicates whether two numeric values are equal.

Overloads

Equals(UInt64)

Returns a value that indicates whether the current instance and an unsigned 64-bit integer have the same value.

Equals(Object)

Returns a value that indicates whether the current instance and a specified object have the same value.

Equals(BigInteger)

Returns a value that indicates whether the current instance and a specified BigInteger object have the same value.

Equals(Int64)

Returns a value that indicates whether the current instance and a signed 64-bit integer have the same value.

Equals(UInt64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Important

This API is not CLS-compliant.

Returns a value that indicates whether the current instance and an unsigned 64-bit integer have the same value.

C#
[System.CLSCompliant(false)]
public bool Equals(ulong other);

Parameters

other
UInt64

The unsigned 64-bit integer to compare.

Returns

true if the current instance and the unsigned 64-bit integer have the same value; otherwise, false.

Attributes

Examples

The following example compares the approximate distance of several stars from Earth with the distance of Epsilon Indi from Earth to determine whether they are equal. The example uses each overload of the Equals method to test for equality.

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

Remarks

To determine the relationship between the two objects instead of just testing for equality, call the BigInteger.CompareTo(UInt64) method.

Applies to

.NET 9 and other versions
Product Versions
.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
.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

Equals(Object)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Returns a value that indicates whether the current instance and a specified object have the same value.

C#
public override bool Equals(object obj);
C#
public override bool Equals(object? obj);

Parameters

obj
Object

The object to compare.

Returns

true if the obj argument is a BigInteger object, and its value is equal to the value of the current BigInteger instance; otherwise, false.

Examples

The following example defines parallel Object and BigInteger arrays. Each element of one array has the same value as the corresponding element of the second array. As the output from the example shows, the instance in the BigInteger array is considered to be equal to the instance in the Object array only if the latter is a BigInteger and their values are equal.

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

Remarks

If the obj argument is not a BigInteger value, the method returns false. The method returns true only if obj is a BigInteger instance whose value is equal to the current instance.

To determine the relationship between the two objects instead of just testing for equality, call the CompareTo(Object) method.

Applies to

.NET 9 and other versions
Product Versions
.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
.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

Equals(BigInteger)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Returns a value that indicates whether the current instance and a specified BigInteger object have the same value.

C#
public bool Equals(System.Numerics.BigInteger other);

Parameters

other
BigInteger

The object to compare.

Returns

true if this BigInteger object and other have the same value; otherwise, false.

Implements

Examples

The following example compares the approximate distance of several stars from Earth with the distance of Epsilon Indi from Earth to determine whether they are equal. The example uses each overload of the Equals method to test for equality.

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

Remarks

This method implements the IEquatable<T> interface and performs slightly better than Equals(Object) because it does not have to convert the other parameter to a BigInteger object.

To determine the relationship between the two BigInteger objects instead of just testing for equality, call the BigInteger.CompareTo(BigInteger) method.

Applies to

.NET 9 and other versions
Product Versions
.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
.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

Equals(Int64)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Returns a value that indicates whether the current instance and a signed 64-bit integer have the same value.

C#
public bool Equals(long other);

Parameters

other
Int64

The signed 64-bit integer value to compare.

Returns

true if the signed 64-bit integer and the current instance have the same value; otherwise, false.

Examples

The following example instantiates a BigInteger object from each integral type except UInt64. It then calls the Equals(Int64) method to compare the BigInteger value with the original integer value that was passed to the BigInteger constructor. As the output shows, the values are equal in each case.

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

Remarks

If other is a Byte, Int16, Int32, SByte, UInt16, or UInt32 value, it is implicitly converted to an Int64 value when the method is called.

To determine the relationship between the two objects instead of just testing for equality, call the BigInteger.CompareTo(Int64) method.

Applies to

.NET 9 and other versions
Product Versions
.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
.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