The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.
The s parameter contains a number of the form:
[ws][sign]digits[ws]
Items in square brackets ([ and ]) are optional. The following table describes each element.
Element | Description |
|---|
ws
| Optional white space. |
sign
| An optional sign. |
digits
| A sequence of digits ranging from 0 to 9. |
The s parameter is interpreted using the NumberStyles..::.Integer style. In addition to the decimal digits, only leading and trailing spaces together with a leading sign are allowed. To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Int32..::.TryParse(String, NumberStyles, IFormatProvider, Int32%) method.
The s parameter is parsed using the formatting information in a NumberFormatInfo object initialized for the current system culture. For more information, see CurrentInfo.
This overload of the TryParse method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Int32..::.TryParse(String, NumberStyles, IFormatProvider, Int32%) overload.