BigInteger.TryParse Method (String, BigInteger)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded.

Namespace:   System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

public static bool TryParse(
	string value,
	out BigInteger result


Type: System.String

The string representation of a number.

Type: System.Numerics.BigInteger

When this method returns, contains the BigInteger equivalent to the number that is contained in value, or zero (0) if the conversion fails. The conversion fails if the value parameter is null or is not of the correct format. This parameter is passed uninitialized.

Return Value

Type: System.Boolean

true if value was converted successfully; otherwise, false.

Exception Condition

value is null.

The TryParse(String, BigInteger) method is like the Parse(String) method, except that it does not throw an exception if the conversion fails. This method eliminates the need to use exception handling to test for a FormatException if value is invalid and cannot be successfully parsed.

The value parameter should be the string representation of a decimal number in the following form:


Elements in square brackets ([ and ]) are optional. The following table describes each element.




Optional white space.


An optional sign. Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture.


A sequence of decimal digits ranging from 0 to 9.


The string specified by the value parameter cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The value parameter is interpreted by using the NumberStyles.Integer style. In addition to the decimal digits, only leading and trailing spaces with a leading sign are allowed. To explicitly define the style elements with the culture-specific formatting information that can be present in value, call the TryParse(String, NumberStyles, IFormatProvider, BigInteger) method.

The value parameter is parsed by using the formatting information in a NumberFormatInfo object for the current culture. For more information, see NumberFormatInfo.CurrentInfo.

This overload interprets all digits in the value parameter as decimal digits. To parse the string representation of a hexadecimal number, call the TryParse(String, NumberStyles, IFormatProvider, BigInteger) overload instead.

The following example uses the TryParse(String, BigInteger) method to instantiate two BigInteger objects. If the conversions succeed, it multiplies each object by another number and then calls the Compare method to determine the relationship between the two objects.

BigInteger number1, number2;
bool succeeded1 = BigInteger.TryParse("-12347534159895123", out number1);
bool succeeded2 = BigInteger.TryParse("987654321357159852", out number2);
if (succeeded1 && succeeded2)
   number1 *= 3;
   number2 *= 2;
   switch (BigInteger.Compare(number1, number2))
      case -1:
         Console.WriteLine("{0} is greater than {1}.", number2, number1);
      case 0:
         Console.WriteLine("{0} is equal to {1}.", number1, number2);
      case 1:
         Console.WriteLine("{0} is greater than {1}.", number1, number2);
   if (! succeeded1) 
      Console.WriteLine("Unable to initialize the first BigInteger value.");

   if (! succeeded2)
      Console.WriteLine("Unable to initialize the second BigInteger value.");
// The example displays the following output:
//      1975308642714319704 is greater than -37042602479685369.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top