BigInteger.Multiply(BigInteger, BigInteger) 方法

定義

傳回兩個 BigInteger 值的乘積。

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

參數

left
BigInteger

要相乘的第一個數字。

right
BigInteger

要相乘的第二個數字。

傳回

leftright 參數的乘積。

範例

下列範例會嘗試使用兩個長整數執行乘法。 由於結果超過長整數的範圍,因此會擲回 , OverflowExceptionMultiply 呼叫 方法來處理乘法。 請注意,C# 會要求您使用 checked 關鍵詞 (,如本範例) 或 /checked+ 編譯程序選項,以確保在數值溢位時擲回例外狀況。

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());
   }
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

備註

此方法 Multiply 是針對不支援運算符多載的語言所實作。 其行為與使用乘法運算子的乘法相同。 此外, Multiply 方法是在具現化 BigInteger 變數時,藉由指派乘法所產生的產品,來取代乘法運算符,如下列範例所示。

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

如有必要,這個方法會自動對物件執行其他整數類型的 BigInteger 隱含轉換。 這會在下一節的範例中說明,其中 Multiply 方法會傳遞兩 Int64 個值。

適用於

另請參閱