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

Description

Byte TryParse(String, NumberStyles, IFormatProvider, Byte) converts the string representation of a number in a specified style and culture-specific format to its Byte equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

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


public static bool TryParse(
  string s,//from w  w  w.  j a va  2  s  .c  om
  NumberStyles style,
  IFormatProvider provider,
  out byte result
)

Parameters

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

  • s - A string containing a number to convert. The string is interpreted using the style specified by style.
  • style - A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.
  • provider - An object that supplies culture-specific formatting information about s. If provider is null, the thread current culture is used.
  • result - When this method returns, contains the 8-bit unsigned 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 of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Returns

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

Example

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


/*from w  ww  . j a v a2s. c o m*/
using System;
using System.Globalization;

public class MainClass
{
   public static void Main()
   {
      string byteString; 
      NumberStyles styles;

      byteString = "1024";
      styles = NumberStyles.Integer;
      CallTryParse(byteString, styles);

      byteString = "100.1";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(byteString, styles);

      byteString = "100.0";
      CallTryParse(byteString, styles);

      byteString = "+100";
      styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign 
               | NumberStyles.AllowTrailingSign;
      CallTryParse(byteString, styles);

      byteString = "-100";
      CallTryParse(byteString, styles);

      byteString = "000000000000000100";
      CallTryParse(byteString, styles);

      byteString = "00,100";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(byteString, styles);

      byteString = "2E+3   ";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(byteString, styles);

      byteString = "FF";
      styles = NumberStyles.HexNumber;
      CallTryParse(byteString, styles);

      byteString = "0x1F";
      CallTryParse(byteString, styles);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {  
      Byte byteValue;
      bool result = Byte.TryParse(stringToConvert, styles, 
                                  null as IFormatProvider, out byteValue);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}", 
                        stringToConvert, byteValue);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           stringToConvert.ToString());
   }
}

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