C# SByte Parse(String, IFormatProvider)

Description

SByte Parse(String, IFormatProvider) converts the string representation of a number in a specified culture-specific format to its 8-bit signed integer equivalent.

Syntax

SByte.Parse(String, IFormatProvider) has the following syntax.


[CLSCompliantAttribute(false)]/*from  w  ww. j a  v  a2 s.c  o m*/
public static sbyte Parse(
  string s,
  IFormatProvider provider
)

Parameters

SByte.Parse(String, IFormatProvider) has the following parameters.

  • s - A string that represents a number to convert. The string is interpreted using the NumberStyles.Integer style.
  • provider - An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.

Returns

SByte.Parse(String, IFormatProvider) method returns An 8-bit signed integer that is equivalent to the number specified in s.

Example

The following example defines a custom NumberFormatInfo object that defines the tilde (~) as the negative sign.


//from   w ww .  jav  a 2s . co m
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo nf = new NumberFormatInfo();
      nf.NegativeSign = "~"; 

      string[] values = { "-103", "+12", "~16", "  1", "~255" };
      IFormatProvider[] providers = { nf, CultureInfo.InvariantCulture };

      foreach (IFormatProvider provider in providers)
      {
         Console.WriteLine("Conversions using {0}:", ((object) provider).GetType().Name);
         foreach (string value in values)
         {
            try {
               Console.WriteLine("   Converted '{0}' to {1}.", 
                                 value, SByte.Parse(value, provider));
            }                     
            catch (FormatException) {
               Console.WriteLine("   Unable to parse '{0}'.", value);   
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
            }
         }
      }      
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version