Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# int.Parse Vs int.TryParse Method
The int.Parse and int.TryParse methods in C# are used to convert string representations of numbers to integers. The key difference lies in how they handle conversion failures: int.Parse throws an exception when the string cannot be converted, while int.TryParse returns a boolean value indicating success or failure.
Syntax
Following is the syntax for int.Parse method −
int result = int.Parse(string);
Following is the syntax for int.TryParse method −
bool success = int.TryParse(string, out int result);
Using int.Parse Method
The int.Parse method directly converts a valid string to an integer. If the conversion fails, it throws a FormatException −
Example
using System;
class Program {
static void Main() {
int res;
string myStr = "120";
res = int.Parse(myStr);
Console.WriteLine("String is a numeric representation: " + res);
}
}
The output of the above code is −
String is a numeric representation: 120
Using int.TryParse Method
The int.TryParse method attempts to convert a string to an integer and returns true if successful, false otherwise. The converted value is returned through the out parameter −
Example
using System;
class Program {
static void Main() {
bool res;
int a;
string myStr = "120";
res = int.TryParse(myStr, out a);
Console.WriteLine("Conversion successful: " + res);
Console.WriteLine("Converted value: " + a);
}
}
The output of the above code is −
Conversion successful: True Converted value: 120
Handling Invalid Conversions
Here's how both methods handle invalid string inputs −
Example
using System;
class Program {
static void Main() {
string invalidStr = "abc123";
// Using TryParse - safe approach
if (int.TryParse(invalidStr, out int result)) {
Console.WriteLine("TryParse succeeded: " + result);
} else {
Console.WriteLine("TryParse failed - invalid string");
}
// Using Parse with exception handling
try {
int parseResult = int.Parse(invalidStr);
Console.WriteLine("Parse succeeded: " + parseResult);
} catch (FormatException) {
Console.WriteLine("Parse failed - FormatException thrown");
}
}
}
The output of the above code is −
TryParse failed - invalid string Parse failed - FormatException thrown
Comparison
| Feature | int.Parse | int.TryParse |
|---|---|---|
| Return Type | int | bool |
| On Success | Returns converted integer | Returns true, value via out parameter |
| On Failure | Throws FormatException | Returns false, sets out parameter to 0 |
| Performance | Slightly faster for valid inputs | Better for uncertain inputs |
| Use Case | When input is guaranteed valid | When input validity is uncertain |
Conclusion
Use int.Parse when you're confident the string is a valid number, and int.TryParse when you need to handle potentially invalid input gracefully. int.TryParse is generally preferred for user input validation as it avoids exception handling overhead and provides cleaner error handling.
