System.UInt16.Parse Method

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

Syntax

[System.CLSCompliant(false)]
public static ushort Parse (string s)

Parameters

s
A string that represents the number to convert.

Returns

A 16-bit unsigned integer equivalent to the number contained in s.

Exceptions

TypeReason
ArgumentNullException s is a null reference.
FormatException s is not in the correct style.
OverflowException s represents a number greater than ushort.MaxValue or less than ushort.MinValue.

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.

ws

Optional white space.

sign

An optional sign. Valid sign characters are determined by the System.Globalization.NumberFormatInfo.NegativeSign and System.Globalization.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.

Note:

The string specified by the s parameter is interpreted by using the System.Globalization.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 System.Globalization.NumberFormatInfo.CurrentInfo. To parse a string by using the formatting information of a specific culture, use the ushort.Parse(string, IFormatProvider) method.

Example

This example demonstrates parsing a string to a ushort.

C# Example

using System;
public class UInt16ParseClass {
  public static void Main() {
    string str = "  100   ";
    Console.WriteLine("String: \"{0}\" <UInt16> {1}",str,UInt16.Parse(str));
  }
}

The output is

String: " 100 " <UInt16> 100

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0