C# Int64 TryParse(String, Int64)
Description
Int64 TryParse(String, Int64)
converts the string representation
of a number to its 64-bit signed integer equivalent. A return value indicates
whether the conversion succeeded or failed.
Syntax
Int64.TryParse(String, Int64)
has the following syntax.
public static bool TryParse(
string s,
out long result
)
Parameters
Int64.TryParse(String, Int64)
has the following parameters.
s
- A string containing a number to convert.result
- When this method returns, contains the 64-bit signed integer value equivalent of 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
Int64.TryParse(String, Int64)
method returns true if s was converted successfully; otherwise, false.
Example
The following example calls the Int32.TryParse(String, Int32) method with a number of different string values.
//ww w . ja va 2 s . c om
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("123456");
TryToParse("9876.0");
TryToParse("12,345");
TryToParse(" -322 ");
TryToParse("+4321");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
The code above generates the following result.