Export (0) Print
Expand All

Int64.TryParse Method (String, NumberStyles, IFormatProvider, Int64%)

Converts the string representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool TryParse(
	string s,
	NumberStyles style,
	IFormatProvider provider,
	out long result
)

Parameters

s
Type: System.String
A string containing a number to convert.
style
Type: System.Globalization.NumberStyles
A bitwise combination of NumberStyles values that indicates the permitted format of s. A typical value to specify is Integer.
provider
Type: System.IFormatProvider
An IFormatProvider object that supplies culture-specific formatting information about s.
result
Type: System.Int64%
When this method returns, contains the 64-bit signed 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, is not in a format compliant with style, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if s was converted successfully; otherwise, false.

ExceptionCondition
ArgumentException

style is not a NumberStyles value.

-or-

style is not a combination of AllowHexSpecifier and HexNumber values.

The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Or, if the style parameter includes AllowHexSpecifier:

[ws]hexdigits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space if permitted by style.

sign

An optional sign.

digits

A sequence of digits from 0 through 9.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

The provider parameter is an IFormatProvider object that obtains a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of s. If provider is null, the NumberFormatInfo object for the current culture is used.

The following code example demonstrates overloads of the TryParse method for several base types, as well as the DateTime.TryParseExact method.


// This example demonstrates overloads of the TryParse method for
// several base types, and the TryParseExact method for DateTime.

// In most cases, this example uses the most complex overload; that is, the overload 
// with the most parameters for a particular type. If a complex overload specifies 
// null (Nothing in Visual Basic) for the IFormatProvider parameter, formatting 
// information is obtained from the culture associated with the current thread. 
// If a complex overload specifies the style parameter, the parameter value is 
// the default value used by the equivalent simple overload.

using System;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      bool result;
      CultureInfo ci;
      string nl = "\n";
      string msg1 =
               "This example demonstrates overloads of the TryParse method for{0}" +
               "several base types, as well as the TryParseExact method for DateTime.{0}";
      string msg2 = "Non-numeric types:{0}";
      string msg3 = "{0}Numeric types:{0}";
      string msg4 = "{0}The following types are not CLS-compliant:{0}";

      // Non-numeric types.
      Boolean booleanVal;
      Char charVal;
      DateTime datetimeVal;

      // Numeric types.
      Byte byteVal;
      Int16 int16Val;
      Int32 int32Val;
      Int64 int64Val;
      Decimal decimalVal;
      Single singleVal;
      Double doubleVal;

      // The following types are not CLS-compliant.
      SByte sbyteVal;
      UInt16 uint16Val;
      UInt32 uint32Val;
      UInt64 uint64Val;
      //
      outputBlock.Text += String.Format(msg1, nl) + "\n";

      // Non-numeric types:
      outputBlock.Text += String.Format(msg2, nl) + "\n";
      // DateTime
      // TryParse:
      // Assume current culture is en-US, and dates of the form: MMDDYYYY.
      result = DateTime.TryParse("7/4/2004 12:34:56", out datetimeVal);
      Show(outputBlock, result, "DateTime #1", datetimeVal.ToString());

      // Use fr-FR culture, and dates of the form: DDMMYYYY.
      ci = new CultureInfo("fr-FR");
      result = DateTime.TryParse("4/7/2004 12:34:56",
               ci, DateTimeStyles.None, out datetimeVal);
      Show(outputBlock, result, "DateTime #2", datetimeVal.ToString());

      // TryParseExact:
      // Use fr-FR culture. The format, "G", is short date and long time.
      result = DateTime.TryParseExact("04/07/2004 12:34:56", "G",
               ci, DateTimeStyles.None, out datetimeVal);
      Show(outputBlock, result, "DateTime #3", datetimeVal.ToString());

      // Assume en-US culture.
      string[] dateFormats = { "f", "F", "g", "G" };
      result = DateTime.TryParseExact("7/4/2004 12:34:56 PM",
               dateFormats, null, DateTimeStyles.None,
               out datetimeVal);
      Show(outputBlock, result, "DateTime #4", datetimeVal.ToString());

      outputBlock.Text += "\n";
      // Boolean
      result = Boolean.TryParse("true", out booleanVal);
      Show(outputBlock, result, "Boolean", booleanVal.ToString());
      // Char
      result = Char.TryParse("A", out charVal);
      Show(outputBlock, result, "Char", charVal.ToString());

      // Numeric types:
      outputBlock.Text += String.Format(msg3, nl) + "\n";
      // Byte
      result = Byte.TryParse("1", NumberStyles.Integer, null, out byteVal);
      Show(outputBlock, result, "Byte", byteVal.ToString());
      // Int16
      result = Int16.TryParse("-2", NumberStyles.Integer, null, out int16Val);
      Show(outputBlock, result, "Int16", int16Val.ToString());
      // Int32
      result = Int32.TryParse("3", NumberStyles.Integer, null, out int32Val);
      Show(outputBlock, result, "Int32", int32Val.ToString());
      // Int64
      result = Int64.TryParse("4", NumberStyles.Integer, null, out int64Val);
      Show(outputBlock, result, "Int64", int64Val.ToString());
      // Decimal
      result = Decimal.TryParse("-5.5", NumberStyles.Number, null, out decimalVal);
      Show(outputBlock, result, "Decimal", decimalVal.ToString());
      // Single
      result = Single.TryParse("6.6",
               (NumberStyles.Float | NumberStyles.AllowThousands),
               null, out singleVal);
      Show(outputBlock, result, "Single", singleVal.ToString());
      // Double
      result = Double.TryParse("-7",
               (NumberStyles.Float | NumberStyles.AllowThousands),
               null, out doubleVal);
      Show(outputBlock, result, "Double", doubleVal.ToString());

      // Use the simple Double.TryParse overload, but specify an invalid value.

      result = Double.TryParse("abc", out doubleVal);
      Show(outputBlock, result, "Double #2", doubleVal.ToString());
      //
      outputBlock.Text += String.Format(msg4, nl) + "\n";
      // SByte
      result = SByte.TryParse("-8", NumberStyles.Integer, null, out sbyteVal);
      Show(outputBlock, result, "SByte", sbyteVal.ToString());
      // UInt16
      result = UInt16.TryParse("9", NumberStyles.Integer, null, out uint16Val);
      Show(outputBlock, result, "UInt16", uint16Val.ToString());
      // UInt32
      result = UInt32.TryParse("10", NumberStyles.Integer, null, out uint32Val);
      Show(outputBlock, result, "UInt32", uint32Val.ToString());
      // UInt64
      result = UInt64.TryParse("11", NumberStyles.Integer, null, out uint64Val);
      Show(outputBlock, result, "UInt64", uint64Val.ToString());
   }

   protected static void Show(System.Windows.Controls.TextBlock outputBlock, bool parseResult, string typeName,
                              string parseValue)
   {
      string msgSuccess = "Parse for {0} = {1}";
      string msgFailure = "** Parse for {0} failed. Invalid input.";
      //
      if (parseResult == true)
         outputBlock.Text += String.Format(msgSuccess, typeName, parseValue) + "\n";
      else
         outputBlock.Text += String.Format(msgFailure, typeName) + "\n";
   }
}
/*
This example produces the following results:

This example demonstrates overloads of the TryParse method for
several base types, as well as the TryParseExact method for DateTime.

Non-numeric types:

Parse for DateTime #1 = 7/4/2004 12:34:56 PM
Parse for DateTime #2 = 7/4/2004 12:34:56 PM
Parse for DateTime #3 = 7/4/2004 12:34:56 PM
Parse for DateTime #4 = 7/4/2004 12:34:56 PM

Parse for Boolean = True
Parse for Char = A

Numeric types:

Parse for Byte = 1
Parse for Int16 = -2
Parse for Int32 = 3
Parse for Int64 = 4
Parse for Decimal = -5.5
Parse for Single = 6.6
Parse for Double = -7
** Parse for Double #2 failed. Invalid input.

The following types are not CLS-compliant:

Parse for SByte = -8
Parse for UInt16 = 9
Parse for UInt32 = 10
Parse for UInt64 = 11
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft