Home

ushortTryParse

ushort.TryParse is a method in C# used to attempt converting a string representation of a number into a 16-bit unsigned integer (ushort). It is a member of the System.UInt16 type and is commonly invoked as ushort.TryParse in code. The method returns a boolean indicating success or failure and provides the parsed value through an out parameter.

In its default form, the string-based overload tries to parse the input using the current culture and

There are overloads that allow explicit specification of formatting rules via a NumberStyles value and an

Usage patterns are straightforward: declare a ushort variable, then attempt parsing and branch on the result.

standard
numeric
styles.
It
returns
true
if
the
entire
input
is
a
valid
representation
of
a
value
within
the
range
0
to
65535;
otherwise
it
returns
false.
If
parsing
fails,
the
out
parameter
is
set
to
zero.
The
method
does
not
throw
exceptions
for
invalid
input,
making
it
suitable
for
safe,
defensive
parsing.
Null,
empty,
or
whitespace-containing
strings
are
considered
invalid
inputs.
IFormatProvider,
enabling
culture-specific
parsing
or
stricter
rules
(for
example,
disallowing
certain
separators).
Modern
frameworks
also
provide
span-based
overloads
for
performance-sensitive
scenarios,
allowing
parsing
from
spans
without
additional
allocations.
For
example,
a
typical
usage
is:
if
(ushort.TryParse(input,
out
var
value))
{
//
use
value
}
else
{
//
handle
invalid
input
}.
The
method
is
designed
to
safely
convert
user
input
or
text
data
into
a
numeric
value
while
avoiding
exceptions
on
failure.