UInt16.Parse Method (String)


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

This API is not CLS-compliant.

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

public static ushort Parse(
	string 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.

Exception Condition

s is null.


s is not in the correct format.


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

The s parameter should be the string representation of a 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. However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.


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


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.

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

using System;

public class Example
   public static void Main()
      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);
            Console.WriteLine("'{0}' --> {1}", value, number);
         catch (FormatException) {
            Console.WriteLine("'{0}' --> Bad Format", value);
         catch (OverflowException) {   
            Console.WriteLine("'{0}' --> OverflowException", value);
         catch (ArgumentNullException) {
            Console.WriteLine("'{0}' --> Null", value);
// 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

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