BigInteger.Explicit Operator

Definition

Defines an explicit conversion between a BigInteger object and another type.

Overloads

Explicit(BigInteger to SByte)

Defines an explicit conversion of a BigInteger object to a signed 8-bit value.

This API is not CLS-compliant. The compliant alternative is Int16.

Explicit(Single to BigInteger)

Defines an explicit conversion of a Single value to a BigInteger value.

Explicit(Complex to BigInteger)

Explicitly converts a Complex value to a big integer.

Explicit(BigInteger to UIntPtr)

Explicitly converts a big integer to a UIntPtr value.

Explicit(BigInteger to UInt64)

Defines an explicit conversion of a BigInteger object to an unsigned 64-bit integer value.

This API is not CLS-compliant. The compliant alternative is Double.

Explicit(BigInteger to UInt32)

Defines an explicit conversion of a BigInteger object to an unsigned 32-bit integer value.

This API is not CLS-compliant. The compliant alternative is Int64.

Explicit(BigInteger to UInt16)

Defines an explicit conversion of a BigInteger object to an unsigned 16-bit integer value.

This API is not CLS-compliant. The compliant alternative is Int32.

Explicit(BigInteger to UInt128)

Explicitly converts a big integer to a UInt128 value.

Explicit(BigInteger to Single)

Defines an explicit conversion of a BigInteger object to a single-precision floating-point value.

Explicit(BigInteger to IntPtr)

Explicitly converts a big integer to a IntPtr value.

Explicit(BigInteger to Int64)

Defines an explicit conversion of a BigInteger object to a 64-bit signed integer value.

Explicit(BigInteger to Int16)

Defines an explicit conversion of a BigInteger object to a 16-bit signed integer value.

Explicit(BigInteger to Int128)

Explicitly converts a big integer to a Int128 value.

Explicit(BigInteger to Half)

Explicitly converts a big integer to a Half value.

Explicit(BigInteger to Double)

Defines an explicit conversion of a BigInteger object to a Double value.

Explicit(BigInteger to Decimal)

Defines an explicit conversion of a BigInteger object to a Decimal value.

Explicit(BigInteger to Char)

Explicitly converts a big integer to a Char value.

Explicit(BigInteger to Byte)

Defines an explicit conversion of a BigInteger object to an unsigned byte value.

Explicit(Half to BigInteger)

Explicitly converts a Half value to a big integer.

Explicit(Double to BigInteger)

Defines an explicit conversion of a Double value to a BigInteger value.

Explicit(Decimal to BigInteger)

Defines an explicit conversion of a Decimal object to a BigInteger value.

Explicit(BigInteger to Int32)

Defines an explicit conversion of a BigInteger object to a 32-bit signed integer value.

Explicit(BigInteger to SByte)

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

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int16

Defines an explicit conversion of a BigInteger object to a signed 8-bit value.

This API is not CLS-compliant. The compliant alternative is Int16.

C#
[System.CLSCompliant(false)]
public static explicit operator sbyte(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a signed 8-bit value.

Returns

An object that contains the value of the value parameter.

Attributes

Exceptions

value is less than SByte.MinValue or is greater than SByte.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to SByte values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the SByte data type.

C#
// BigInteger to SByte conversion.
BigInteger goodSByte = BigInteger.MinusOne;
BigInteger badSByte = -130;

sbyte sByteFromBigInteger;

// Successful conversion using cast operator.
sByteFromBigInteger = (sbyte) goodSByte;
Console.WriteLine(sByteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   sByteFromBigInteger = (sbyte) badSByte;
   Console.WriteLine(sByteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badSByte, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CSByte in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the SByte data type. There is no loss of precision in the resulting SByte value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(Single to BigInteger)

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

Defines an explicit conversion of a Single value to a BigInteger value.

C#
public static explicit operator System.Numerics.BigInteger(float value);

Parameters

value
Single

The value to convert to a BigInteger.

Returns

An object that contains the value of the value parameter.

Exceptions

Examples

The following example converts the individual elements in an array of Single values to BigInteger objects, and then displays the result of each conversion. Note that any fractional part of a Single value is truncated during the conversion.

C#
float[] singles = { Single.MinValue, -1.430955172e03f, 2.410970032e05f,
                    Single.MaxValue, Single.PositiveInfinity,
                     Single.NegativeInfinity, Single.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Single", "BigInteger");

foreach (float value in singles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//           Single                            BigInteger
//
//    -3.402823E+38   -3.4028234663852885981170418348E+38
//        -1430.955                                 -1430
//           241097                                241097
//     3.402823E+38    3.4028234663852885981170418348E+38
//         Infinity                     OverflowException
//        -Infinity                     OverflowException
//              NaN                     OverflowException

Remarks

Any fractional part of the value parameter is truncated before conversion.

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Because the conversion from Single to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. Otherwise, they display a compiler error.

For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Single).

Applies to

.NET 10 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, 10
.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

Explicit(Complex to BigInteger)

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

Explicitly converts a Complex value to a big integer.

C#
public static explicit operator System.Numerics.BigInteger(System.Numerics.Complex value);

Parameters

value
Complex

The value to convert.

Returns

value converted to a big integer.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to UIntPtr)

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

Important

This API is not CLS-compliant.

Explicitly converts a big integer to a UIntPtr value.

C#
[System.CLSCompliant(false)]
public static explicit operator UIntPtr(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

UIntPtr

value converted to UIntPtr value.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to UInt64)

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

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Double

Defines an explicit conversion of a BigInteger object to an unsigned 64-bit integer value.

This API is not CLS-compliant. The compliant alternative is Double.

C#
[System.CLSCompliant(false)]
public static explicit operator ulong(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to an unsigned 64-bit integer.

Returns

An object that contains the value of the value parameter.

Attributes

Exceptions

value is less than UInt64.MinValue or is greater than UInt64.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to UInt64 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt64 data type.

C#
// BigInteger to UInt64 conversion.
BigInteger goodULong = 2000000000;
BigInteger badULong = BigInteger.Pow(goodULong, 3);

ulong uLongFromBigInteger;

// Successful conversion using cast operator.
uLongFromBigInteger = (ulong) goodULong;
Console.WriteLine(uLongFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uLongFromBigInteger = (ulong) badULong;
   Console.WriteLine(uLongFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badULong, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CULng in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt64 data type. There is no loss of precision in the resulting UInt64 value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to UInt32)

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

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int64

Defines an explicit conversion of a BigInteger object to an unsigned 32-bit integer value.

This API is not CLS-compliant. The compliant alternative is Int64.

C#
[System.CLSCompliant(false)]
public static explicit operator uint(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to an unsigned 32-bit integer.

Returns

An object that contains the value of the value parameter.

Attributes

Exceptions

value is less than UInt32.MinValue or is greater than UInt32.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to UInt32 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt32 data type.

C#
// BigInteger to UInt32 conversion.
BigInteger goodUInteger = 200000;
BigInteger badUInteger = 65000000000;

uint uIntegerFromBigInteger;

// Successful conversion using cast operator.
uIntegerFromBigInteger = (uint) goodInteger;
Console.WriteLine(uIntegerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uIntegerFromBigInteger = (uint) badUInteger;
   Console.WriteLine(uIntegerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badUInteger, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CUInt in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt32 data type. There is no loss of precision in the resulting UInt32 value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to UInt16)

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

Important

This API is not CLS-compliant.

CLS-compliant alternative
System.Int32

Defines an explicit conversion of a BigInteger object to an unsigned 16-bit integer value.

This API is not CLS-compliant. The compliant alternative is Int32.

C#
[System.CLSCompliant(false)]
public static explicit operator ushort(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to an unsigned 16-bit integer.

Returns

An object that contains the value of the value parameter.

Attributes

Exceptions

value is less than UInt16.MinValue or is greater than UInt16.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to UInt16 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt16 data type.

C#
// BigInteger to UInt16 conversion.
BigInteger goodUShort = 20000;
BigInteger badUShort = 66000;

ushort uShortFromBigInteger;

// Successful conversion using cast operator.
uShortFromBigInteger = (ushort) goodUShort;
Console.WriteLine(uShortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uShortFromBigInteger = (ushort) badUShort;
   Console.WriteLine(uShortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badUShort, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CUShort in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt16 data type. There is no loss of precision in the resulting UInt16 value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to UInt128)

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

Important

This API is not CLS-compliant.

Explicitly converts a big integer to a UInt128 value.

C#
[System.CLSCompliant(false)]
public static explicit operator UInt128(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

value converted to UInt128 value.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to Single)

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

Defines an explicit conversion of a BigInteger object to a single-precision floating-point value.

C#
public static explicit operator float(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a single-precision floating-point value.

Returns

An object that contains the closest possible representation of the value parameter.

Examples

The following example illustrates the conversion of BigInteger to Single values.

C#
// BigInteger to Single conversion.
BigInteger goodSingle = (BigInteger) 102.43e22F;
BigInteger badSingle = (BigInteger) float.MaxValue;
badSingle = badSingle * 2;

float singleFromBigInteger;

// Successful conversion using cast operator.
singleFromBigInteger = (float) goodSingle;
Console.WriteLine(singleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = (float) badSingle;
Console.WriteLine(singleFromBigInteger);

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss or a loss of precision. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CSng in Visual Basic) is used. Otherwise, they display a compiler error.

Because the BigInteger value can be outside the range of the Single data type, this operation is a narrowing conversion. If the conversion is unsuccessful, it does not throw an OverflowException. Instead, if the BigInteger value is less than Single.MinValue, the resulting Single value is Single.NegativeInfinity. If the BigInteger value is greater than Single.MaxValue, the resulting Single value is Single.PositiveInfinity.

The conversion of a BigInteger to a Single may involve a loss of precision. In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Single data type. The following example provides an illustration. It assigns the maximum value of a Single to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. As expected, the call to the BigInteger.Equals(BigInteger) method shows that they are unequal. However, the conversion of the larger BigInteger value back to a Single succeeds, although the BigInteger value now exceeds Single.MaxValue.

C#
// Increase a BigInteger so it exceeds Single.MaxValue.
BigInteger number1 = (BigInteger) Single.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e30;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Single.
float sng = (float) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Single:     {0}", sng);
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 3.4028235663752885981170396038E+38
//       Single:     3.402823E+38

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to IntPtr)

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

Explicitly converts a big integer to a IntPtr value.

C#
public static explicit operator IntPtr(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

IntPtr

value converted to IntPtr value.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to Int64)

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

Defines an explicit conversion of a BigInteger object to a 64-bit signed integer value.

C#
public static explicit operator long(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a 64-bit signed integer.

Returns

An object that contains the value of the value parameter.

Exceptions

value is less than Int64.MinValue or is greater than Int64.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to Int64 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int64 data type.

C#
// BigInteger to Int64 conversion.
BigInteger goodLong = 2000000000;
BigInteger badLong = BigInteger.Pow(goodLong, 3);

long longFromBigInteger;

// Successful conversion using cast operator.
longFromBigInteger = (long) goodLong;
Console.WriteLine(longFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   longFromBigInteger = (long) badLong;
   Console.WriteLine(longFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badLong, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CLng in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int64 data type.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to Int16)

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

Defines an explicit conversion of a BigInteger object to a 16-bit signed integer value.

C#
public static explicit operator short(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a 16-bit signed integer.

Returns

An object that contains the value of the value parameter.

Exceptions

value is less than Int16.MinValue or is greater than Int16.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to Int16 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int16 data type.

C#
// BigInteger to Int16 conversion.
BigInteger goodShort = 20000;
BigInteger badShort = 33000;

short shortFromBigInteger;

// Successful conversion using cast operator.
shortFromBigInteger = (short) goodShort;
Console.WriteLine(shortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   shortFromBigInteger = (short) badShort;
   Console.WriteLine(shortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badShort, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CShort in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int16 data type. There is no loss of precision in the resulting Int16 value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to Int128)

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

Explicitly converts a big integer to a Int128 value.

C#
public static explicit operator Int128(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

value converted to Int128 value.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to Half)

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

Explicitly converts a big integer to a Half value.

C#
public static explicit operator Half(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

value converted to Half value.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to Double)

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

Defines an explicit conversion of a BigInteger object to a Double value.

C#
public static explicit operator double(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a Double.

Returns

An object that contains the value of the value parameter.

Examples

The following example illustrates the conversion of BigInteger to Double values.

C#
// BigInteger to Double conversion.
BigInteger goodDouble = (BigInteger) 102.43e22;
BigInteger badDouble = (BigInteger) Double.MaxValue;
badDouble = badDouble * 2;

double doubleFromBigInteger;

// successful conversion using cast operator.
doubleFromBigInteger = (double) goodDouble;
Console.WriteLine(doubleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = (double) badDouble;
Console.WriteLine(doubleFromBigInteger);

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CDbl in Visual Basic) is used.

Because the BigInteger value can be outside the range of the Double data type, this operation is a narrowing conversion. If the conversion is unsuccessful, it does not throw an OverflowException. Instead, if the BigInteger value is less than Double.MinValue, the resulting Double value is Double.NegativeInfinity. If the BigInteger value is greater than Double.MaxValue, the resulting Double value is Double.PositiveInfinity.

The conversion of a BigInteger to a Double may involve a loss of precision. In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Double data type. The following example provides an illustration. It assigns the maximum value of a Double to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. As expected, the call to the BigInteger.Equals(BigInteger) method shows that they are unequal. However, the conversion of the larger BigInteger value back to a Double succeeds, although the BigInteger value now exceeds Double.MaxValue.

C#
// Increase a BigInteger so it exceeds Double.MaxValue.
BigInteger number1 = (BigInteger) Double.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e291;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Double.
double dbl = (double) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Double:     {0}", dbl);
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 1.7976931348623158081352742373E+308
//       Double:     1.79769313486232E+308

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to Decimal)

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

Defines an explicit conversion of a BigInteger object to a Decimal value.

C#
public static explicit operator decimal(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a Decimal.

Returns

An object that contains the value of the value parameter.

Exceptions

value is less than Decimal.MinValue or greater than Decimal.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to Decimal values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Decimal data type.

C#
// BigInteger to Decimal conversion.
BigInteger goodDecimal = 761652543;
BigInteger badDecimal = (BigInteger) Decimal.MaxValue;
badDecimal += BigInteger.One;

Decimal decimalFromBigInteger;

// Successful conversion using cast operator.
decimalFromBigInteger = (decimal) goodDecimal;
Console.WriteLine(decimalFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   decimalFromBigInteger = (decimal) badDecimal;
   Console.WriteLine(decimalFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badDecimal, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CDec in Visual Basic) is used.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Decimal data type.

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to Char)

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

Explicitly converts a big integer to a Char value.

C#
public static explicit operator char(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert.

Returns

value converted to Char value.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(BigInteger to Byte)

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

Defines an explicit conversion of a BigInteger object to an unsigned byte value.

C#
public static explicit operator byte(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a Byte.

Returns

An object that contains the value of the value parameter.

Exceptions

value is less than Byte.MinValue or greater than Byte.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to Byte values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Byte data type.

C#
// BigInteger to Byte conversion.
BigInteger goodByte = BigInteger.One;
BigInteger badByte = 256;

byte byteFromBigInteger;

// Successful conversion using cast operator.
byteFromBigInteger = (byte) goodByte;
Console.WriteLine(byteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   byteFromBigInteger = (byte) badByte;
   Console.WriteLine(byteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badByte, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CByte in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Byte data type. There is no loss of precision in the resulting Byte value if the conversion is successful.

Applies to

.NET 10 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, 10
.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

Explicit(Half to BigInteger)

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

Explicitly converts a Half value to a big integer.

C#
public static explicit operator System.Numerics.BigInteger(Half value);

Parameters

value
Half

The value to convert.

Returns

value converted to a big integer.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Explicit(Double to BigInteger)

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

Defines an explicit conversion of a Double value to a BigInteger value.

C#
public static explicit operator System.Numerics.BigInteger(double value);

Parameters

value
Double

The value to convert to a BigInteger.

Returns

An object that contains the value of the value parameter.

Exceptions

Examples

The following example converts the individual elements in an array of Double values to BigInteger objects, and then displays the result of each conversion. Note that any fractional part of a Double value is truncated during the conversion.

C#
double[] doubles = { Double.MinValue, -1.430955172e03, 2.410970032e05,
                     Double.MaxValue, Double.PositiveInfinity,
                     Double.NegativeInfinity, Double.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Double", "BigInteger");

foreach (double value in doubles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//                                Double                            BigInteger
//
//                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
//                          -1430.955172                                 -1430
//                           241097.0032                                241097
//                 1.79769313486232E+308   1.7976931348623157081452742373E+308
//                              Infinity                     OverflowException
//                             -Infinity                     OverflowException
//                                   NaN                     OverflowException

Remarks

Any fractional part of the value parameter is truncated before conversion.

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Because the conversion from Double to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. Otherwise, they display a compiler error.

For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Double).

Applies to

.NET 10 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, 10
.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

Explicit(Decimal to BigInteger)

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

Defines an explicit conversion of a Decimal object to a BigInteger value.

C#
public static explicit operator System.Numerics.BigInteger(decimal value);

Parameters

value
Decimal

The value to convert to a BigInteger.

Returns

An object that contains the value of the value parameter.

Examples

The following example converts the individual elements in an array of Decimal values to BigInteger objects, and then displays the result of each conversion. Note that any fractional part of a Decimal value is truncated during the conversion.

C#
decimal[] decimals = { Decimal.MinValue, -15632.991m, 9029321.12m,
                       Decimal.MaxValue };
BigInteger number;

Console.WriteLine("{0,35} {1,35}\n", "Decimal", "BigInteger");

foreach (decimal value in decimals)
{
   number = (BigInteger) value;
   Console.WriteLine("{0,35} {1,35}", value, number);
}
// The example displays the following output:
//
//                          Decimal                          BigInteger
//
//    -79228162514264337593543950335      -79228162514264337593543950335
//                       -15632.991                              -15632
//                       9029321.12                             9029321
//    79228162514264337593543950335       79228162514264337593543950335

Remarks

Any fractional part of the value parameter is truncated before conversion.

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Because the conversion from Decimal to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. Otherwise, they display a compiler error.

For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Decimal).

Applies to

.NET 10 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, 10
.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

Explicit(BigInteger to Int32)

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

Defines an explicit conversion of a BigInteger object to a 32-bit signed integer value.

C#
public static explicit operator int(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to convert to a 32-bit signed integer.

Returns

An object that contains the value of the value parameter.

Exceptions

value is less than Int32.MinValue or is greater than Int32.MaxValue.

Examples

The following example illustrates the conversion of BigInteger to Int32 values. It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int32 data type.

C#
// BigInteger to Int32 conversion.
BigInteger goodInteger = 200000;
BigInteger badInteger = 65000000000;

int integerFromBigInteger;

// Successful conversion using cast operator.
integerFromBigInteger = (int) goodInteger;
Console.WriteLine(integerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   integerFromBigInteger = (int) badInteger;
   Console.WriteLine(integerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}",
                     badInteger, e.Message);
}
Console.WriteLine();

Remarks

The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. Language compilers do not perform this conversion automatically because it can involve data loss. Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CInt in Visual Basic) is used. Otherwise, they display a compiler error.

Because this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int32 data type. There is no loss of precision in the resulting Int32 value if the conversion is successful.

Applies to

.NET 10 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, 10
.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