This topic has not yet been rated - Rate this topic

SByte.Parse Method (String)

April 12, 2014

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 {
      outputBlock.Text += String.Format("Converted '{0}' to the SByte value {1}.",
                        value, SByte.Parse(value)) + Environment.NewLine;
   }
   catch (FormatException) {
      outputBlock.Text += String.Format("'{0}' cannot be parsed successfully by SByte type.",
                        value) + Environment.NewLine;
   }                              
   catch (OverflowException) {
      outputBlock.Text += String.Format("'{0}' is out of range of the SByte type.",
                        value) + Environment.NewLine;
   }                                                                        
}
// 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 Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.