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
How to convert a string into int in C#?
Converting a string to an integer in C# is a common task that can be accomplished using several methods. The most straightforward approach is using Int32.Parse() method, which converts the string representation of a number to its 32-bit signed integer equivalent.
Syntax
Following is the syntax for using Int32.Parse() method −
int result = Int32.Parse(stringValue);
Following is the syntax for using Convert.ToInt32() method −
int result = Convert.ToInt32(stringValue);
Following is the syntax for using int.TryParse() method −
bool success = int.TryParse(stringValue, out int result);
Using Int32.Parse() Method
The Int32.Parse() method is the most commonly used approach for converting strings to integers −
using System;
class Demo {
static void Main() {
string str = "9999";
int n = Int32.Parse(str);
Console.WriteLine("Original string: " + str);
Console.WriteLine("Converted integer: " + n);
Console.WriteLine("Type: " + n.GetType());
}
}
The output of the above code is −
Original string: 9999 Converted integer: 9999 Type: System.Int32
Using Convert.ToInt32() Method
The Convert.ToInt32() method provides another way to convert strings to integers and handles null values gracefully −
using System;
class Program {
static void Main() {
string str1 = "12345";
string str2 = null;
int num1 = Convert.ToInt32(str1);
int num2 = Convert.ToInt32(str2); // returns 0 for null
Console.WriteLine("String '12345' converts to: " + num1);
Console.WriteLine("Null string converts to: " + num2);
}
}
The output of the above code is −
String '12345' converts to: 12345 Null string converts to: 0
Using int.TryParse() for Safe Conversion
The int.TryParse() method is the safest approach as it doesn't throw exceptions for invalid input −
using System;
class SafeConversion {
static void Main() {
string[] testStrings = {"123", "abc", "999999999999", "42.5"};
foreach (string str in testStrings) {
if (int.TryParse(str, out int result)) {
Console.WriteLine($"'{str}' successfully converted to: {result}");
} else {
Console.WriteLine($"'{str}' could not be converted to integer");
}
}
}
}
The output of the above code is −
'123' successfully converted to: 123 'abc' could not be converted to integer '999999999999' could not be converted to integer '42.5' could not be converted to integer
Comparison of Methods
| Method | Exception on Invalid Input | Handles Null | Best Use Case |
|---|---|---|---|
| Int32.Parse() | Yes (throws FormatException) | No (throws ArgumentNullException) | When input is guaranteed to be valid |
| Convert.ToInt32() | Yes (throws FormatException) | Yes (returns 0) | When null values need to be handled |
| int.TryParse() | No (returns false) | Yes (returns false) | When input validation is uncertain |
Conclusion
Converting strings to integers in C# can be done using Int32.Parse(), Convert.ToInt32(), or int.TryParse(). Use TryParse() for safe conversion when input validity is uncertain, and Parse() when you're confident the input is valid.
