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 Method
The int.Parse method in C# converts a string representation of a number to an integer. If the string cannot be converted to a valid integer, the method throws a FormatException.
Syntax
Following is the basic syntax for int.Parse −
int result = int.Parse(stringValue);
The method also has overloaded versions that accept additional parameters −
int result = int.Parse(stringValue, NumberStyles.Integer); int result = int.Parse(stringValue, CultureInfo.InvariantCulture);
Parameters
-
s − A string containing a number to convert.
-
style (optional) − A bitwise combination of enumeration values that indicates the style elements.
-
provider (optional) − An object that supplies culture-specific formatting information.
Return Value
Returns a 32-bit signed integer equivalent to the number contained in the string parameter.
Using int.Parse with Valid String
Example
using System;
class Program {
static void Main() {
string myStr = "200";
int result = int.Parse(myStr);
Console.WriteLine("String '" + myStr + "' converted to integer: " + result);
string negativeStr = "-150";
int negativeResult = int.Parse(negativeStr);
Console.WriteLine("String '" + negativeStr + "' converted to integer: " + negativeResult);
}
}
The output of the above code is −
String '200' converted to integer: 200 String '-150' converted to integer: -150
Exception Handling with int.Parse
When the string cannot be parsed as an integer, int.Parse throws a FormatException. It's good practice to handle this exception −
Example
using System;
class Program {
static void Main() {
string[] testStrings = {"123", "abc", "45.67", ""};
foreach(string str in testStrings) {
try {
int result = int.Parse(str);
Console.WriteLine("Successfully parsed '" + str + "' to: " + result);
}
catch(FormatException) {
Console.WriteLine("Failed to parse '" + str + "' - invalid format");
}
}
}
}
The output of the above code is −
Successfully parsed '123' to: 123 Failed to parse 'abc' - invalid format Failed to parse '45.67' - invalid format Failed to parse '' - invalid format
Using int.TryParse as Alternative
For safer parsing without exceptions, consider using int.TryParse which returns true if parsing succeeds −
Example
using System;
class Program {
static void Main() {
string[] values = {"100", "invalid", "250"};
foreach(string value in values) {
int result;
bool success = int.TryParse(value, out result);
if(success) {
Console.WriteLine("Parsed '" + value + "' successfully: " + result);
} else {
Console.WriteLine("Could not parse '" + value + "'");
}
}
}
}
The output of the above code is −
Parsed '100' successfully: 100 Could not parse 'invalid' Parsed '250' successfully: 250
Comparison
| Method | Exception on Failure | Return Value | Best Used When |
|---|---|---|---|
| int.Parse | Yes (FormatException) | Parsed integer | Input is expected to be valid |
| int.TryParse | No | bool (success/failure) | Input might be invalid |
Conclusion
The int.Parse method converts string representations of numbers to integers but throws exceptions for invalid input. For robust applications, consider using int.TryParse or proper exception handling to manage conversion failures gracefully.
