Try to parse string to byte in CSharp

Description

The following code shows how to try to parse string to byte.

Example


  //w ww .  j ava2s .  c  o m
  
  
  
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
        string[] numericStrings = {"-13.16", "112.18", "    131   ", "(17)", "1,124", "  1127 "};
        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);
        }
   }
}




















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