Export (0) Print
Expand All

SByte.Parse Method (String)

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.

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft