System.UInt16.Parse Method

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

Syntax

[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style)

Parameters

s
A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.
style
A bitwise combination of the enumeration values that specify the permitted format of s. A typical value to specify is System.Globalization.NumberStyles.Integer.
s
A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.
style
A bitwise combination of the enumeration values that specify the permitted format of s. A typical value to specify is System.Globalization.NumberStyles.Integer.

Returns

A 16-bit unsigned integer equivalent to the number specified 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 style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the s parameter for the parse operation to succeed. style must be a combination of bit flags from the System.Globalization.NumberStyles enumeration. The style parameter makes this method overload useful when s contains the string representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by s is known only at run time, or when you want to disallow white space or a sign symbol in s.

Depending on the value of style, the s parameter may include the following elements:

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

Elements in square brackets ([ and ]) are optional. If style includes System.Globalization.NumberStyles.AllowHexSpecifier, the s parameter may contain the following elements:

[ws]hexdigits[ws]

The following table describes each element.

ws

Optional white space. White space can appear at the start of s if style includes the System.Globalization.NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the System.Globalization.NumberStyles.AllowTrailingWhite flag.

$

A culture-specific currency symbol. Its position in the string is defined by the System.Globalization.NumberFormatInfo.CurrencyNegativePattern and System.Globalization.NumberFormatInfo.CurrencyPositivePattern properties of the current culture. The current culture's currency symbol can appear in s if style includes the System.Globalization.NumberStyles.AllowCurrencySymbol flag.

sign

An optional sign. The sign can appear at the start of s if style includes the System.Globalization.NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the System.Globalization.NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the System.Globalization.NumberStyles.AllowParentheses flag. However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.

digits

fractional_digits

exponential_digits

A sequence of digits from 0 through 9. For fractional_digits, only the digit 0 is valid.

,

A culture-specific group separator symbol. The current culture's group separator can appear in s if style includes the System.Globalization.NumberStyles.AllowThousands flag.

.

A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s if style includes the System.Globalization.NumberStyles.AllowDecimalPoint flag. Only the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.

E

The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The s parameter can represent a number in exponential notation if style includes the System.Globalization.NumberStyles.AllowExponent flag.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

A string with digits only (which corresponds to the System.Globalization.NumberStyles.None style) always parses successfully. Most of the remaining System.Globalization.NumberStyles members control elements that may be present, but are not required to be present, in the input string. The following table indicates how individual System.Globalization.NumberStyles members affect the elements that may be present in s.

System.Globalization.NumberStyles.None

The digits element only.

System.Globalization.NumberStyles.AllowDecimalPoint

The decimal point (.) and fractional-digits elements.

System.Globalization.NumberStyles.AllowExponent

The "e" or "E" character, which indicates exponential notation, along with exponential_digits.

System.Globalization.NumberStyles.AllowLeadingWhite

The ws element at the start of s.

System.Globalization.NumberStyles.AllowTrailingWhite

The ws element at the end of s.

System.Globalization.NumberStyles.AllowLeadingSign

The sign element at the start of s.

System.Globalization.NumberStyles.AllowTrailingSign

The sign element at the end of s.

System.Globalization.NumberStyles.AllowParentheses

The sign element in the form of parentheses enclosing the numeric value.

System.Globalization.NumberStyles.AllowThousands

The group separator (,) element.

System.Globalization.NumberStyles.AllowCurrencySymbol

The currency ($) element.

System.Globalization.NumberStyles.Currency

All elements. However, s cannot represent a hexadecimal number or a number in exponential notation.

System.Globalization.NumberStyles.Float

The ws element at the start or end of s, sign at the start of s, and the decimal point (.) symbol. The s parameter can also use exponential notation.

System.Globalization.NumberStyles.Number

The ws, sign, group separator (,), and decimal point (.) elements.

System.Globalization.NumberStyles.Any

All elements. However, s cannot represent a hexadecimal number.

Unlike the other System.Globalization.NumberStyles values, which allow for, but do not require, the presence of particular style elements in s, the System.Globalization.NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in s are always interpreted as hexadecimal characters. Valid hexadecimal characters are 0-9, A-F, and a-f. The only other flags that can be combined with the style parameter are System.Globalization.NumberStyles.AllowLeadingWhite and System.Globalization.NumberStyles.AllowTrailingWhite. (The System.Globalization.NumberStyles enumeration includes a composite number style, System.Globalization.NumberStyles.HexNumber, that includes both white-space flags.)

Note:

If s is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. This causes the conversion to fail.

The s parameter is parsed by using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. To specify the culture whose formatting information is used for the parse operation, call the ushort.Parse(string, System.Globalization.NumberStyles, IFormatProvider) overload.

Requirements

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