A 16-bit unsigned integer equivalent to the number contained in s.
Type Reason 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.
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. |
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.
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