BigInteger.Multiply(BigInteger, BigInteger) Metoda

Definicja

Zwraca iloczyn dwóch BigInteger wartości.

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

Parametry

left
BigInteger

Pierwsza liczba do pomnożenia.

right
BigInteger

Druga liczba do pomnożenia.

Zwraca

Iloczyn parametrów left i right .

Przykłady

Poniższy przykład próbuje wykonać mnożenie z dwoma długimi liczbami całkowitymi. Ponieważ wynik przekracza zakres długiej liczby całkowitej, OverflowException element jest zgłaszany, a Multiply metoda jest wywoływana w celu obsługi mnożenia. Pamiętaj, że język C# wymaga użycia słowa kluczowego checked (jak w tym przykładzie) lub /checked+ opcji kompilatora, aby upewnić się, że wyjątek jest zgłaszany w przepełnieniu liczbowym.

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

Uwagi

Metoda Multiply jest implementowana dla języków, które nie obsługują przeciążenia operatora. Jego zachowanie jest identyczne z mnożeniem przy użyciu operatora mnożenia. Ponadto Multiply metoda jest użytecznym substytutem operatora mnożenia podczas tworzenia wystąpienia BigInteger zmiennej, przypisując mu produkt, który wynika z mnożenia, jak pokazano w poniższym przykładzie.

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

W razie potrzeby ta metoda automatycznie wykonuje niejawną konwersję innych typów całkowitych na BigInteger obiekty. Jest to zilustrowane w przykładzie w następnej sekcji, gdzie metoda jest przekazywana Multiply dwie Int64 wartości.

Dotyczy

Zobacz też