int.Parse() / int.TryParse()
| Since: | C# 1.0(2002) |
|---|
Converts a string to an integer using int.Parse(), or attempts the conversion safely without throwing an exception using int.TryParse().
Syntax
int.Parse(string s) // Attempts the conversion. Returns true and the converted value on success. Does not throw an exception on failure. int.TryParse(string s, out int result) // Similar methods exist for other numeric types such as double and long. double.Parse(string s) double.TryParse(string s, out double result)
Method List
| Method | Description |
|---|---|
| int.Parse(string s) | Converts a string to an int. Throws a FormatException if the string cannot be converted. |
| int.TryParse(string s, out int result) | Attempts the conversion. Returns true and sets result to the converted value on success. Returns false and sets result to 0 on failure. |
| double.Parse(string s) | Converts a string to a double. |
| double.TryParse(string s, out double result) | Attempts to convert a string to a double. |
Sample Code
Program.cs
using System;
// Convert a string to an integer using int.Parse()
string numStr = "42";
int num = int.Parse(numStr);
Console.WriteLine(num + 10); // 52
// Convert safely using int.TryParse()
string input1 = "100";
string input2 = "abc"; // A string that cannot be converted to a number
if (int.TryParse(input1, out int result1)) {
Console.WriteLine("Conversion succeeded: " + result1); // Conversion succeeded: 100
}
if (!int.TryParse(input2, out int result2)) {
Console.WriteLine("Conversion failed: " + result2); // Conversion failed: 0
}
This produces the following output:
dotnet run 52 Conversion succeeded: 100 Conversion failed: 0
Converting User Input
Using TryParse() is a common pattern when you want to use user input as an integer. Similar methods are available for other numeric types such as double and long.
UserInput.cs
using System;
// A typical pattern for converting user input to an integer
Console.Write("Enter your score: ");
string userInput = Console.ReadLine() ?? "";
if (int.TryParse(userInput, out int score)) {
Console.WriteLine($"Your score is {score}.");
} else {
Console.WriteLine("Please enter a valid number.");
}
// Example using double.Parse()
double ratio = double.Parse("0.75");
Console.WriteLine($"Achievement rate: {ratio:P0}"); // Achievement rate: 75%
// Converting to long
if (long.TryParse("9999999999", out long bigNum)) {
Console.WriteLine(bigNum); // 9999999999
}
This produces the following output:
dotnet run Enter your score: 88 Your score is 88. Achievement rate: 75% 9999999999
Common Mistakes
Common Mistake: Passing a Non-Convertible String to int.Parse()
Passing a non-numeric string to int.Parse() throws a FormatException. Always use TryParse() for user input.
using System;
// NG: Passing a non-convertible string throws an exception
int value = int.Parse("abc"); // FormatException
dotnet run Unhandled exception. System.FormatException: Input string was not in a correct format.
using System;
// OK: Use TryParse() to convert safely without exceptions
if (int.TryParse("abc", out int value)) {
Console.WriteLine(value);
} else {
Console.WriteLine("Conversion failed.");
}
This produces the following output:
dotnet run Conversion failed.
Notes
When converting user input or external data, using int.TryParse() is safer than int.Parse(). If the input is not a valid number, int.Parse() throws an exception and halts the program, whereas int.TryParse() lets you handle the failure safely.
Convert.ToInt32() can also convert a string to an integer, but it differs in that passing null returns 0 instead of throwing an exception. For details, see Convert.ToString() / Convert.ToInt32().
If you find any errors or copyright issues, please contact us.