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

Description

UInt16 TryParse(String, NumberStyles, IFormatProvider, UInt16) tries to convert the string representation of a number in a specified style and culture-specific format to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

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


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

Returns

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

Example

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


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

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

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

      numericString = "-10603";
      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 = "2123E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 

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

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

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

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

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      ushort number;
      bool result = UInt16.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