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

Description

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

Syntax

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


[CLSCompliantAttribute(false)]/* ww  w.ja  v  a  2  s . c  om*/
public static bool TryParse(
  string s,
  NumberStyles style,
  IFormatProvider provider,
  out ulong result
)

Parameters

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

  • s - A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.
  • style - A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is NumberStyles.Integer.
  • provider - An object that supplies culture-specific formatting information about s.
  • result - When this method returns, contains the 64-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 in a format compliant with style, or represents a number less than UInt64.MinValue or greater than UInt64.MaxValue. This parameter is passed uninitialized.

Returns

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

Example

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


using System;/*  w w w.  j a v a2 s.  c o  m*/
using System.Globalization;

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

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

      numericString = "-12343";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);

      numericString = "23423423.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "12345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "42342310E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 

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

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

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

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

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