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 參數的乘積。

範例

下列範例會嘗試使用兩個長整數執行乘法。 因為結果超過長整數的範圍, OverflowException 所以會擲回 ,並 Multiply 呼叫 方法來處理乘法。 請注意,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 個值。

適用於

另請參閱