C# UInt16 Parse(String)
Description
UInt16 Parse(String)
converts the string representation
of a number to its 16-bit unsigned integer equivalent.
Syntax
UInt16.Parse(String)
has the following syntax.
[CLSCompliantAttribute(false)]
public static ushort Parse(
string s
)
Parameters
UInt16.Parse(String)
has the following parameters.
s
- A string that represents the number to convert.
Returns
UInt16.Parse(String)
method returns A 16-bit unsigned integer equivalent to the number contained in s.
Example
The following example calls the Parse(String) method to convert each element in a string array to an unsigned 16-bit integer.
using System;//from w ww . j a va 2 s.c om
public class Example
{
public static void Main()
{
string[] values = { "-0", "17", "-12", "123", "12345", "+0",
"", null, "12.1", "12.0", "1,234" };
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 code above generates the following result.