This documentation is archived and is not being maintained.

Byte.TryParse Method (String, Byte%)

Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

static member TryParse : 
        s:string * 
        result:byte byref -> bool 


Type: System.String
A string that contains a number to convert. The string is interpreted using the Integer style.
Type: System.Byte%
When this method returns, contains the Byte value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if s was converted successfully; otherwise, false.

The conversion fails and the method returns false if the s parameter is not in the correct format, if it is a null reference (Nothing in Visual Basic), or if it represents a number less than MinValue or greater than MaxValue.

The Byte.TryParse(String, Byte) method is similar to the Byte.Parse(String) method, except that TryParse(String, Byte) does not throw an exception if the conversion fails.

The s parameter should be the string representation of a number in the following form:


Elements in square brackets ([ and ]) are optional. The following table describes each element.




Optional white space.


An optional positive sign, as specified by the NumberFormatInfo.PositiveSign property of the current culture.


A sequence of decimal digits that range from 0 to 9.

The s parameter is interpreted using the Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Byte.Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is parsed using the formatting information in a NumberFormatInfo object for the current culture. For more information, see NumberFormatInfo.CurrentInfo.

This overload of the Byte.TryParse(String, Byte) method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) overload.

The following example calls the TryParse(String, Byte) method with a number of different string values.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.