System.SByte.Parse Method

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

Syntax

[System.CLSCompliant(false)]
public static sbyte Parse (string s)

Parameters

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

Returns

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

Exceptions

TypeReason
ArgumentException s is a null reference.
FormatException s is not in the correct style.
OverflowException s represents a number greater than sbyte.MaxValue or less than sbyte.MinValue.

Remarks

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.

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 System.Globalization.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 sbyte.Parse(string, System.Globalization.NumberStyles) or the sbyte.Parse(string, System.Globalization.NumberStyles, IFormatProvider) method.

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

Example

This example demonstrates the sbyte.Parse(string)(string) method.

C# Example

using System;
public class SByteParseClass {
  public static void Main() {
    string str = "  100   ";
    Console.WriteLine("String: \"{0}\" <SByte> {1}",
                      str,SByte.Parse(str));
  }
}

The output is

String: " 100 " <SByte> 100

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0