BigInteger.Negate(BigInteger) Method

Definition

Negates a specified BigInteger value.

C#
public static System.Numerics.BigInteger Negate(System.Numerics.BigInteger value);

Parameters

value
BigInteger

The value to negate.

Returns

The result of the value parameter multiplied by negative one (-1).

Examples

The following example illustrates three ways to negate the value of a BigInteger object.

C#
BigInteger number = 12645002;

Console.WriteLine(BigInteger.Negate(number));        // Displays -12645002
Console.WriteLine(-number);                          // Displays -12645002
Console.WriteLine(number * BigInteger.MinusOne);     // Displays -12645002

Remarks

Negation obtains the additive inverse of a number. The additive inverse of a number is a number that produces a value of zero when it is added to the original number.

The Negate method is implemented for languages that do not support custom operators. Its behavior is identical to negation using the unary negation operator. In addition, the Negate method is a useful substitute for the negation operator when instantiating a BigInteger variable, as shown in the following example.

C#
// The statement
//    BigInteger number = -Int64.MinValue;
// produces compiler error CS0220: The operation overflows at compile time in checked mode.
// The alternative:
BigInteger number = BigInteger.Negate(Int64.MinValue);

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also