This documentation is archived and is not being maintained.

UInt16.TryParse Method (String, UInt16%)

Updated: August 2009

Converts the string representation of a number to its 16-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 TryParse(String, Int32).

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

<CLSCompliantAttribute(False)> _
Public Shared Function TryParse ( _
	s As String, _
	<OutAttribute> ByRef result As UShort _
) As Boolean
Dim s As String 
Dim result As UShort 
Dim returnValue As Boolean 

returnValue = UShort.TryParse(s, result)


Type: System.String

A string that represents the number to convert.

Type: System.UInt16%

When this method returns, contains the 16-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 Nothing, is not in the correct format. , or represents a number less than UInt16.MinValue or greater than UInt16.MaxValue. This parameter is passed uninitialized.

Return Value

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

The TryParse(String, UInt16) 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:


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 s parameter cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

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, UInt16) method.

The s parameter is parsed by using the formatting information in a NumberFormatInfo object 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, UInt16) overload instead.

The following example calls the TryParse(String, UInt16) 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 UInteger
For Each numericString As String In numericStrings
   If UInt32.TryParse(numericString, number) Then
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
      Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString)
   End If       
' The example displays the following output: 
'       Cannot convert '1293.8' to a UInt32. 
'       Cannot convert '+1671.7' to a UInt32. 
'       Cannot convert '28347.' to a UInt32. 
'       Converted '   33113684  ' to 33113684. 
'       Cannot convert '(0)' to a UInt32. 
'       Converted '-0' to 0. 
'       Converted '+1293617' to 1293617. 
'       Cannot convert '18-' to a UInt32. 
'       Converted '119870' to 119870. 
'       Cannot convert '31,024' to a UInt32. 
'       Converted '  3127094 ' to 3127094. 
'       Converted '0070000' to 70000.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0




August 2009

Revised extensively.

Information enhancement.