Int64.Parse(String) Method in C# with Examples
Int64.Parse(String) Method is used to convert the string representation of a number to its 64-bit signed integer equivalent.
Syntax:
public static long Parse (string str);
Here, str is a string containing a number to convert. The format of str will be [optional white space][optional sign]digits[optional white space].
Return Value: It is a 64-bit signed integer equivalent to the number contained in str.
Exceptions:
- ArgumentNullException: If str is null.
- FormatException: If str is not in the correct format.
- OverflowException: If str represents a number less than MinValue or greater than MaxValue.
Below programs illustrate the use of above-discussed method:
Example 1:
// C# program to demonstrate // Int64.Parse(String) Method using System; class GFG { // Main Method public static void Main() { // passing different values // to the method to check checkParse("9223372036854775807"); checkParse("29,22337,20368547"); checkParse("-922337203685477"); checkParse(" 9223372075807"); } // Defining checkParse method public static void checkParse(string input) { try { // declaring Int64 variable long val; // getting parsed value val = Int64.Parse(input); Console.WriteLine("'{0}' parsed as {1}", input, val); } catch (FormatException) { Console.WriteLine("Can't Parsed '{0}'", input); } } } |
'9223372036854775807' parsed as 9223372036854775807 Can't Parsed '29,22337,20368547' '-922337203685477' parsed as -922337203685477 ' 9223372075807' parsed as 9223372075807
Example 2: For ArgumentNullException
// C# program to demonstrate // Int64.Parse(String) Method // for ArgumentNullException using System; class GFG { // Main Method public static void Main() { try { // passing null value as a input checkParse(null); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (FormatException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Defining checkparse method public static void checkParse(string input) { // declaring Int64 variable long val; // getting parsed value val = Int64.Parse(input); Console.WriteLine("'{0}' parsed as {1}", input, val); } } |
Exception Thrown: System.ArgumentNullException
Reference:
Recommended Posts:
- MathF.Abs() Method in C# with Examples
- MathF.Tan() Method in C# with Examples
- MathF.Log() Method in C# with Examples
- MathF.Min() Method in C# with Examples
- MathF.Exp() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- MathF.Max() Method in C# with Examples
- MathF.Cos() Method in C# with Examples
- MathF.Pow() Method in C# with Examples
- MathF.Acosh() Method in C# with Examples
- MathF.Asinh() Method in C# with Examples
- Single.CompareTo() Method in C# with Examples
- List.FindIndex() Method in C# with Examples
- Type.GetConstructors() Method in C# with Examples
- C# | Char.GetTypeCode() Method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



