C# SByte TryParse(String, SByte)

Description

SByte TryParse(String, SByte) tries to convert the string representation of a number to its SByte equivalent, and returns a value that indicates whether the conversion succeeded.

Syntax

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


[CLSCompliantAttribute(false)]/*from  w w  w. j a  v  a  2s . c  o  m*/
public static bool TryParse(
  string s,
  out sbyte result
)

Parameters

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

  • s - A string that contains a number to convert.
  • result - When this method returns, contains the 8-bit signed integer value that is 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 the correct format, or represents a number that is less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Returns

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

Example

The following example tries to convert the strings in an array to SByte values by calling the TryParse(String, SByte) method.


/*from   w ww .  j av  a  2s .  c om*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string[] numericStrings = {"-3.4", "12.3", "+12.3", "    3   ", "(12)", 
                               "-12", "+12", "18-", "987", "1,024"};
    sbyte number;
    foreach (string numericString in numericStrings)
    {
       if (sbyte.TryParse(numericString, out number)) 
          Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
       else
          Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString);
    }

  }
}
    

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