Parse string to byte in CSharp

Description

The following code shows how to parse string to byte.

Example


/*  w w w . j  a  v  a  2 s. c o m*/
using System;
public class MainClass{
    public static void Main(){

        string[] values = { "-116", "  -3", "+120", "(1111)", "1111", "-130" };
        
        foreach (string value in values)
        {
           try {
              Console.WriteLine("Converted '{0}' to the SByte value {1}.",
                                value, SByte.Parse(value));
           }
           catch (FormatException) {
              Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
                                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 »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var