Muokkaa

Jaa


BigInteger.Multiply(BigInteger, BigInteger) Method

Definition

Returns the product of two BigInteger values.

public:
 static System::Numerics::BigInteger Multiply(System::Numerics::BigInteger left, System::Numerics::BigInteger right);
public static System.Numerics.BigInteger Multiply (System.Numerics.BigInteger left, System.Numerics.BigInteger right);
static member Multiply : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
Public Shared Function Multiply (left As BigInteger, right As BigInteger) As BigInteger

Parameters

left
BigInteger

The first number to multiply.

right
BigInteger

The second number to multiply.

Returns

The product of the left and right parameters.

Examples

The following example tries to perform multiplication with two long integers. Because the result exceeds the range of a long integer, an OverflowException is thrown, and the Multiply method is called to handle the multiplication. Note that C# requires that you use either the checked keyword (as in this example) or the /checked+ compiler option to make sure an exception is thrown on a numeric overflow.

long number1 = 1234567890;
long number2 = 9876543210;
try
{
   long product;
   product = checked(number1 * number2);
}
catch (OverflowException)
{
   BigInteger product;
   product = BigInteger.Multiply(number1, number2);
   Console.WriteLine(product.ToString());
   }
let number1 = 1234567890L
let number2 = 9876543210L

try

    let product: int64 = Checked.(*) number1 number2
    ()
with :? OverflowException ->
    let product = BigInteger.Multiply(number1, number2)
    printfn $"{product}"
Dim number1 As Long = 1234567890
Dim number2 As Long = 9876543210
Try
   Dim product As Long
   product = number1 * number2
   Console.WriteLine(product.ToString("N0"))
Catch e As OverflowException
   Dim product As BigInteger
   product = BigInteger.Multiply(number1, number2)
   Console.WriteLine(product.ToString)
End Try

Remarks

The Multiply method is implemented for languages that do not support operator overloading. Its behavior is identical to multiplication using the multiplication operator. In addition, the Multiply method is a useful substitute for the multiplication operator when instantiating a BigInteger variable by assigning it a product that results from multiplication, as shown in the following example.

// The statement
//    BigInteger number = Int64.MaxValue * 3;
// produces compiler error CS0220: The operation overflows at compile time in checked mode.
// The alternative:
BigInteger number = BigInteger.Multiply(Int64.MaxValue, 3);
let number = BigInteger.Multiply(Int64.MaxValue, 3);
' The statement
'    Dim number As BigInteger = Int64.MaxValue * 3
' produces compiler error BC30439: Constant expression not representable in type 'Long'.
' The alternative:
Dim number As BigInteger = BigInteger.Multiply(Int64.MaxValue, 3)

If necessary, this method automatically performs implicit conversion of other integral types to BigInteger objects. This is illustrated in the example in the next section, where the Multiply method is passed two Int64 values.

Applies to

See also