Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Int32.Parse — converting text to int

Int32.Parse converts numeric text to int. Learn its default format rules, how culture affects parsing, and which exceptions invalid input produces.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

Int32.Parse(string s)
Int32.Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider)

Int32.Parse is a static method returning int, which is System.Int32. Writing int.Parse calls the same method. The result ranges from -2147483648 to 2147483647. The described overloads are available in .NET 10; they require no preview language features.

Int32.Parse(string s) uses the current culture and NumberStyles.Integer: it accepts decimal digits, a leading sign, and leading or trailing whitespace. Group separators and decimal separators are not accepted by default. The style parameter controls permitted formatting, while provider supplies cultural conventions; null means the current culture. For predictable parsing of culture-independent data, supply CultureInfo.InvariantCulture.

For string overloads, null causes ArgumentNullException, invalid formatting causes FormatException, and an out-of-range value causes OverflowException. Invalid style flags cause ArgumentException. When invalid input is expected, choose Int32.TryParse, which signals conversion failure with false.

The example parses " +42 ", then catches the overflow from "2147483648". Explicit culture makes both parsing and output predictable.

Examples

using System;
using System.Globalization;

int value = Int32.Parse(" +42 ", NumberStyles.Integer, CultureInfo.InvariantCulture);
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));

try
{
    Int32.Parse("2147483648", NumberStyles.Integer, CultureInfo.InvariantCulture);
}
catch (OverflowException)
{
    Console.WriteLine("OverflowException");
}

Result

42
OverflowException

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources