System.Int64.Parse Method

Converts the string representation of a number to its 64-bit signed integer equivalent.

Syntax

public static long Parse (string s)

Parameters

s
A string containing a number to convert.

Returns

A 64-bit signed integer equivalent to the number contained in s.

Exceptions

TypeReason
ArgumentNullException s is a null reference.
FormatException s is not in the correct style.
OverflowException s represents a number greater than long.MaxValue or less than long.MinValue.

Remarks

The s parameter contains a number of the form:

[ws][sign]digits[ws]

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

ws

Optional white space.

sign

An optional sign.

digits

A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the System.Globalization.NumberStyles.Integer style. In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed. To explicitly define the style elements that can be present in s, use either the long.Parse(string, System.Globalization.NumberStyles) or the long.Parse(string, System.Globalization.NumberStyles, IFormatProvider) method.

The s parameter is parsed using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. To parse a string using the formatting information of some other culture, use the long.Parse(string, System.Globalization.NumberStyles, IFormatProvider) method.

Example

This example demonstrates parsing a string to a long.

C# Example

using System;
public class Int64ParseClass {
  public static void Main() {
    string str = "  100   ";
    Console.WriteLine("String: \"{0}\" <Int64> {1}",str,Int64.Parse(str));
  }
}

The output is

String: " 100 " <Int64> 100

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0