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
What is the main difference between int.Parse() and Convert.ToInt32 in C#?
Both int.Parse() and Convert.ToInt32() methods in C# are used to convert string representations of numbers to integers. However, they handle null values and certain edge cases differently.
The key difference is that Convert.ToInt32() handles null values gracefully by returning 0, while int.Parse() throws an ArgumentNullException when encountering null.
Syntax
Following is the syntax for int.Parse() method −
int result = int.Parse(stringValue);
Following is the syntax for Convert.ToInt32() method −
int result = Convert.ToInt32(stringValue);
Using int.Parse() with Valid String
The int.Parse() method converts a valid numeric string to an integer −
using System;
class Program {
static void Main() {
int res;
string myStr = "5000";
res = int.Parse(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
}
}
The output of the above code is −
Converting String is a numeric representation: 5000
Using Convert.ToInt32() with Null Value
The Convert.ToInt32() method returns 0 when the input is null −
using System;
class Program {
static void Main() {
int res;
string myStr = null;
res = Convert.ToInt32(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
}
}
The output of the above code is −
Converting String is a numeric representation: 0
Using int.Parse() with Null Value
The int.Parse() method throws an exception when the input is null −
using System;
class Program {
static void Main() {
try {
int res;
string myStr = null;
res = int.Parse(myStr);
Console.WriteLine("Converting String is a numeric representation: " + res);
}
catch (ArgumentNullException ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Error: Value cannot be null. (Parameter 's')
Comparison
| Feature | int.Parse() | Convert.ToInt32() |
|---|---|---|
| Null Handling | Throws ArgumentNullException | Returns 0 |
| Performance | Slightly faster | Slightly slower due to null checks |
| Input Types | Only accepts strings | Accepts various types (object, bool, etc.) |
| Use Case | When you're sure input is not null | When input might be null |
Using Both Methods with Exception Handling
Here's a practical comparison showing both methods handling different scenarios −
using System;
class Program {
static void Main() {
string[] testValues = { "123", null, "456" };
Console.WriteLine("Testing int.Parse():");
foreach (string value in testValues) {
try {
int result = int.Parse(value);
Console.WriteLine($"Parsed '{value}': {result}");
}
catch (ArgumentNullException) {
Console.WriteLine($"int.Parse() failed: null value");
}
}
Console.WriteLine("\nTesting Convert.ToInt32():");
foreach (string value in testValues) {
try {
int result = Convert.ToInt32(value);
Console.WriteLine($"Converted '{value}': {result}");
}
catch (Exception ex) {
Console.WriteLine($"Convert.ToInt32() failed: {ex.Message}");
}
}
}
}
The output of the above code is −
Testing int.Parse(): Parsed '123': 123 int.Parse() failed: null value Parsed '456': 456 Testing Convert.ToInt32(): Converted '123': 123 Converted '': 0 Converted '456': 456
Conclusion
Use int.Parse() when you're certain the input is not null and want better performance. Use Convert.ToInt32() when the input might be null and you want it to default to 0. Both methods throw exceptions for invalid numeric strings, but only Convert.ToInt32() gracefully handles null values.
