BigInteger.Multiply(BigInteger, BigInteger) Método

Definição

Devolve o produto de dois BigInteger valores.

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

Parâmetros

left
BigInteger

O primeiro número a multiplicar.

right
BigInteger

O segundo número a multiplicar.

Devoluções

O produto dos left parâmetros e right .

Exemplos

O exemplo seguinte tenta realizar a multiplicação por dois inteiros longos. Como o resultado excede o intervalo de um inteiro longo, um OverflowException é lançado, e o Multiply método é chamado para tratar da multiplicação. Note que o C# exige que use ou a checked palavra-chave (como neste exemplo) ou a /checked+ opção do compilador para garantir que uma exceção é lançada num excesso numérico.

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

Observações

O Multiply método é implementado para linguagens que não suportam sobrecarga de operadores. O seu comportamento é idêntico ao da multiplicação usando o operador de multiplicação. Além disso, o Multiply método é um substituto útil para o operador de multiplicação ao instanciar uma BigInteger variável atribuindo-lhe um produto que resulta da multiplicação, como mostrado no exemplo seguinte.

// 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)

Se necessário, este método realiza automaticamente a conversão implícita de outros tipos de integrais em BigInteger objetos. Isto é ilustrado no exemplo da secção seguinte, onde o Multiply método recebe dois Int64 valores.

Aplica-se a

Ver também