This documentation is archived and is not being maintained.

SByte.Parse Method (String)

Updated: April 2009

Converts the string representation of a number to its 8-bit signed integer equivalent.

This API is not CLS-compliant. The CLS-compliant alternative is Parse(String).

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

[CLSCompliantAttribute(false)]
public static sbyte Parse(
	string s
)

Parameters

s
Type: System.String

A string that represents a number to convert. The string is interpreted using the NumberStyles.Integer style.

Return Value

Type: System.SByte
An 8-bit signed integer that is equivalent to the number contained in the s parameter.

ExceptionCondition
ArgumentException

s is null.

FormatException

s does not consist of an optional sign followed by a sequence of digits (zero through nine).

OverflowException

s represents a number less than MinValue or greater than MaxValue.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

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

Element

Description

ws

Optional white space.

sign

An optional sign.

digits

A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the NumberStyles.Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces with a leading positive or negative sign are allowed. To explicitly define the style elements that can be present in s, use either the Parse(String, NumberStyles) or the Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is parsed by using the formatting information in a NumberFormatInfo that is initialized for the current system culture. For more information, see NumberFormatInfo.CurrentInfo. To parse a string by using the formatting information of some other culture, use the Parse(String, NumberStyles, IFormatProvider) method.

The following example demonstrates how to convert a string value into a signed byte value using the Parse method. The resulting signed byte value is then displayed to the console.

// Define an array of numeric strings.
string[] values = { "-16", "  -3", "+ 12", " +12 ", "  12  ",
                    "+120", "(103)", "192", "-160" };

// Parse each string and display the result. 
foreach (string value in values)
{
   try {
      Console.WriteLine("Converted '{0}' to the SByte value {1}.",
                        value, SByte.Parse(value));
   }
   catch (FormatException) {
      Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
                        value);
   }                              
   catch (OverflowException) {
      Console.WriteLine("'{0}' is out of range of the SByte type.",
                        value);
   }                                                                        
}
// The example displays the following output: 
//       Converted '-16' to the SByte value -16. 
//       Converted '  -3' to the SByte value -3. 
//       '+ 12' cannot be parsed successfully by SByte type. 
//       Converted ' +12 ' to the SByte value 12. 
//       Converted '  12  ' to the SByte value 12. 
//       Converted '+120' to the SByte value 120. 
//       '(103)' cannot be parsed successfully by SByte type. 
//       '192' is out of range of the SByte type. 
//       '-160' is out of range of the SByte type.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

April 2009

Expanded the Remarks section and replaced the example.

Information enhancement.

Show: