Поделиться через


UInt64.TryParse Method (String, UInt64%)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Tries to convert the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

This API is not CLS-compliant. The CLS-compliant alternative is Parse(String).

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

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function TryParse ( _
    s As String, _
    <OutAttribute> ByRef result As ULong _
) As Boolean
[CLSCompliantAttribute(false)]
public static bool TryParse(
    string s,
    out ulong result
)

Parameters

  • s
    Type: System.String
    A string that represents the number to convert.
  • result
    Type: System.UInt64%
    When this method returns, contains the 64-bit unsigned integer value that is equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is nulla null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than UInt64.MinValue or greater than UInt64.MaxValue. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if s was converted successfully; otherwise, false.

Remarks

The TryParse(String, UInt64%) 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 s is invalid and cannot be successfully parsed.

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

[ws][sign]digits[ws]

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

Term

Definition

ws

An optional white space.

sign

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

digits

A sequence of digits ranging from 0 to 9.

The s 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 s, call the TryParse(String, NumberStyles, IFormatProvider, UInt64%) method.

NoteNote:

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

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

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

Examples

The following example calls the TryParse(String, UInt64%) method once for each element in a string array.

Dim numericStrings() As String = {"1293.8", "+1671.7", "28347.", _
                                  "   33113684  ", "(0)", "-0", "+1293617", _
                                  "18-", "119870", "31,024", "  3127094 ", _
                                  "0070000"}
Dim number As ULong
For Each numericString As String In numericStrings
   If UInt64.TryParse(numericString, number) Then
      outputBlock.Text += String.Format("Converted '{0}' to {1}.", numericString, number) & vbCrLf
   Else
      outputBlock.Text += String.Format("Cannot convert '{0}' to a UInt64.", numericString) & vbCrLf
   End If
Next
' The example displays the following output:
'       Cannot convert '1293.8' to a UInt64.
'       Cannot convert '+1671.7' to a UInt64.
'       Cannot convert '28347.' to a UInt64.
'       Converted '   33113684  ' to 33113684.
'       Cannot convert '(0)' to a UInt64.
'       Converted '-0' to 0.
'       Converted '+1293617' to 1293617.
'       Cannot convert '18-' to a UInt64.
'       Converted '119870' to 119870.
'       Cannot convert '31,024' to a UInt64.
'       Converted '  3127094 ' to 3127094.
'       Converted '0070000' to 70000.
string[] numericStrings = { "1293.8", "+1671.7", "28347.", 
                            "   33113684  ", "(0)", "-0", "+1293617", 
                            "18-", "119870", "31,024", "  3127094 ",  
                            "0070000" };
ulong number;
foreach (string numericString in numericStrings)
{
   if (UInt64.TryParse(numericString, out number))
      outputBlock.Text += String.Format("Converted '{0}' to {1}.", numericString, number) + "\n";
   else
      outputBlock.Text += String.Format("Cannot convert '{0}' to a UInt64.", numericString) + "\n";
}
// The example displays the following output:
//       Cannot convert '1293.8' to a UInt64.
//       Cannot convert '+1671.7' to a UInt64.
//       Cannot convert '28347.' to a UInt64.
//       Converted '   33113684  ' to 33113684.
//       Cannot convert '(0)' to a UInt64.
//       Converted '-0' to 0.
//       Converted '+1293617' to 1293617.
//       Cannot convert '18-' to a UInt64.
//       Converted '119870' to 119870.
//       Cannot convert '31,024' to a UInt64.
//       Converted '  3127094 ' to 3127094.
//       Converted '0070000' to 70000.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.