UInt16.Parse Method (String)

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

Updated: August 2009

Converts the string representation of a number to its 16-bit unsigned integer equivalent.

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 Parse ( _
    s As String _
) As UShort
[CLSCompliantAttribute(false)]
public static ushort Parse(
    string s
)

Parameters

  • s
    Type: System.String
    A string that represents the number to convert.

Return Value

Type: System.UInt16
A 16-bit unsigned integer equivalent to the number contained in s.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

FormatException

s is not in the correct format.

OverflowException

s represents a number less than UInt16.MinValue or greater than UInt16.MaxValue.

Remarks

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

[ws][sign]digits[ws]

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

Element

Description

ws

Optional white space.

sign

An optional sign. Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture. However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.

digits

A sequence of digits ranging from 0 to 9. Any leading zeros are ignored.

NoteNote:

The string specified by the s parameter is interpreted by using the NumberStyles.Integer style. It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The s parameter is parsed by using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. For more information, see NumberFormatInfo.CurrentInfo. To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) method.

Examples

The following example calls the Parse(String) method to convert each element in a string array to an unsigned 16-bit integer.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim values() As String = {"-0", "17", "-12", "185", "66012", _
                                 "+0", "", Nothing, "16.1", "28.0", _
                                 "1,034"}
      For Each value As String In values
         Try
            Dim number As UShort = UInt16.Parse(value)
            outputBlock.Text += String.Format("'{0}' --> {1}", value, number) & vbCrLf
         Catch e As FormatException
            outputBlock.Text += String.Format("'{0}' --> Bad Format", value) & vbCrLf
         Catch e As OverflowException
            outputBlock.Text += String.Format("'{0}' --> OverflowException", value) & vbCrLf
         Catch e As ArgumentNullException
            outputBlock.Text += String.Format("'{0}' --> Null", value) & vbCrLf
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       '-0' --> 0
'       '17' --> 17
'       '-12' --> OverflowException
'       '185' --> 185
'       '66012' --> OverflowException
'       '+0' --> 0
'       '' --> Bad Format
'       '' --> Null
'       '16.1' --> Bad Format
'       '28.0' --> Bad Format
'       '1,034' --> Bad Format
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] values = { "-0", "17", "-12", "185", "66012", "+0", 
                          "", null, "16.1", "28.0", "1,034" };
      foreach (string value in values)
      {
         try
         {
            ushort number = UInt16.Parse(value);
            outputBlock.Text += String.Format("'{0}' --> {1}", value, number) + "\n";
         }
         catch (FormatException)
         {
            outputBlock.Text += String.Format("'{0}' --> Bad Format", value) + "\n";
         }
         catch (OverflowException)
         {
            outputBlock.Text += String.Format("'{0}' --> OverflowException", value) + "\n";
         }
         catch (ArgumentNullException)
         {
            outputBlock.Text += String.Format("'{0}' --> Null", value) + "\n";
         }
      }
   }
}
// The example displays the following output:
//       '-0' --> 0
//       '17' --> 17
//       '-12' --> OverflowException
//       '185' --> 185
//       '66012' --> OverflowException
//       '+0' --> 0
//       '' --> Bad Format
//       '' --> Null
//       '16.1' --> Bad Format
//       '28.0' --> Bad Format
//       '1,034' --> Bad Format

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.

Change History

Date

History

Reason

August 2009

Revised extensively.

Information enhancement.