System.Int32.Parse Method
Converts the string representation of a number to its 32-bit signed integer equivalent.
Namespace
System
Assembly
System.Runtime.dll
Syntax
public static int Parse(string s);
public static int Parse(string s, System.Globalization.NumberStyles style);
public static int Parse(string s, System.IFormatProvider provider);
public static int Parse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider);
Parameters
- s – The string to convert.
- style – A bitwise combination of enumeration values that indicate the style elements that can be present in
s. (Optional) - provider – An object that supplies culture-specific formatting information. (Optional)
Return Value
The 32-bit signed integer equivalent of the number contained in s.
Exceptions
ArgumentNullException–sisnull.FormatException–sis not in a correct format.OverflowException–srepresents a number less thanInt32.MinValueor greater thanInt32.MaxValue.
Remarks
The method uses the formatting conventions of the current culture unless a specific IFormatProvider is supplied. Leading and trailing white‑space characters are ignored.
Interactive Demo
Example
// C# example
try
{
int number = Int32.Parse("12345");
Console.WriteLine(number);
}
catch (FormatException)
{
Console.WriteLine("Input string was not in a correct format.");
}
catch (OverflowException)
{
Console.WriteLine("The number is too large or too small for an Int32.");
}