C# SByte TryParse(String, NumberStyles, IFormatProvider, SByte)

Description

SByte TryParse(String, NumberStyles, IFormatProvider, SByte) tries to convert the string representation of a number in a specified style and culture-specific format to its SByte equivalent, and returns a value that indicates whether the conversion succeeded.

Syntax

SByte.TryParse(String, NumberStyles, IFormatProvider, SByte) has the following syntax.


[CLSCompliantAttribute(false)]/*from www. j  a  va  2 s  .  c  om*/
public static bool TryParse(
  string s,
  NumberStyles style,
  IFormatProvider provider,
  out sbyte result
)

Parameters

SByte.TryParse(String, NumberStyles, IFormatProvider, SByte) has the following parameters.

  • s - A string representing a number to convert.
  • style - A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is Integer.
  • provider - An object that supplies culture-specific formatting information about s.
  • result - When this method returns, contains the 8-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Returns

SByte.TryParse(String, NumberStyles, IFormatProvider, SByte) method returns true if s was converted successfully; otherwise, false.

Example

The following example calls the TryParse(String, NumberStyles, IFormatProvider, SByte) method with a number of different string and NumberStyles values.


using System;/*from   w  w  w.  j  av a2  s  . co m*/
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "123";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

      numericString = "-123";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);

      numericString = "123.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "123.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "10E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 

      numericString = "12E-01";
      CallTryParse(numericString, styles);

      numericString = "12E01";
      CallTryParse(numericString, styles); 

      numericString = "C8";
      CallTryParse(numericString, NumberStyles.HexNumber);

      numericString = "0x8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      sbyte number;
      bool result = SByte.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           Convert.ToString(stringToConvert));
   }
}

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