UInt64.Parse(String) Method in C# with Examples

UInt64.Parse(String) Method is used to convert the string representation of a number to its 64-bit unsigned integer equivalent.

Syntax:

public static ulong 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]. The sign can be positive or negative. But negative sign can be used only with zero otherwise it will throw an OverflowException.



Return Value: It is a 64-bit unsigned 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// UInt64.Parse(String) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // passing different values
        // to the method to check
        checkParse("18446744073709551615");
        checkParse("454654615,784");
        checkParse("-564564564");
        checkParse(" 119846456456  ");
    }
  
    // Defining checkParse method
    public static void checkParse(string input)
    {
  
        try {
  
            // declaring UInt64 variable
            ulong val;
  
            // getting parsed value
            val = UInt64.Parse(input);
            Console.WriteLine("'{0}' parsed as {1}", input, val);
        }
  
        catch (OverflowException) {
  
            Console.WriteLine("Can't Parsed '{0}'", input);
        }
  
        catch (FormatException) {
  
            Console.WriteLine("Can't Parsed '{0}'", input);
        }
    }
}

chevron_right


Output:

'18446744073709551615' parsed as 18446744073709551615
Can't Parsed '454654615,784'
Can't Parsed '-564564564'
' 119846456456  ' parsed as 119846456456

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// UInt64.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 UInt64 variable
        ulong val;
  
        // getting parsed value
        val = UInt64.Parse(input);
        Console.WriteLine("'{0}' parsed as {1}", input, val);
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.