Converting a string to an integer in C# can be done in many ways depending on the specific needs of your application. Here are general ways to do this.
Using int.Parse
The int.Parse method is used to convert a string to an integer. Throw a FormatException if the string is not a valid integer.
Syntax-
string str = "123";
int number = int.Parse(str);
Console.WriteLine(number); // Output: 123
using int.TryParse
The int.TryParse method attempts to convert a string to an integer and returns a boolean indicating whether the conversion was successful or not. This method is safe because it does not throw an exception if the conversion fails.
Syntax-
string str = "123";
bool success = int.TryParse(str, out int number);
if (success)
{
Console.WriteLine(number); // Output: 123
}
else
{
Console.WriteLine("Conversion failed.");
}
Using Convert.ToInt32
The Convert.ToInt32 method converts the specified string position of any number to a 32-bit equivalent signed integer. It handles null values by returning 0 and throws a FormatException if the string is not a valid integer.
Syntax-
string str = "123";
int number = Convert.ToInt32(str);
Console.WriteLine(number); // Output: 123
Handling exceptions and edge cases
When using int.Parse or Convert.ToInt32, you must deal with potential exceptions to avoid runtime errors.
Syntax-
string strNum = "1234rter";
try
{
int num = int.Parse(strNum);
Console.WriteLine("Number is: "+ num);
}
catch (FormatException)
{
Console.WriteLine("The string format is not a valid for integer.");
}
catch (OverflowException)
{
Console.WriteLine("The number is too large or too small for an Int32.");
}
There is no need to handle exceptions in the case of int.tryParse because it returns the boolean value true or false.
int.parse- Direct convert the values so it throws the exceptions
Convert.int32- it handles null gracefully by returning 0, but throws an exception on invalid format.
Example-
The below example illustrate the different way to convert string into int in C#
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
public static void Main()
{
// using int.Parse method
string strNum1 = "1234";
int num1 = int.Parse(strNum1);
Console.WriteLine("Number1 is: "+ num1);
// using int.TryParse methos
string strNum2 = "1234xyz";
bool IsNumber = int.TryParse(strNum2, out int num2);
if(IsNumber)
{
Console.WriteLine("Number2 is: "+ num2);
}
else
{
Console.WriteLine("Conversion failed: string format not valid for number");
}
// using try catch block to handle exception
string strNum3 = "12345abc";
try
{
int num = int.Parse(strNum3);
Console.WriteLine("Number is: "+ num);
}
catch (FormatException)
{
Console.WriteLine("The string format is not a valid for integer.");
}
catch (OverflowException)
{
Console.WriteLine("The number is too large or too small for an Int32.");
}
}
}
Output-
Number1 is: 1234
Conversion failed: string format not valid for number
The string format is not a valid for integer.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Convert string to int in C#
Converting a string to an integer in C# can be done in many ways depending on the specific needs of your application. Here are general ways to do this.
Using int.Parse
The int.Parse method is used to convert a string to an integer. Throw a FormatException if the string is not a valid integer.
Syntax-
using int.TryParse
The int.TryParse method attempts to convert a string to an integer and returns a boolean indicating whether the conversion was successful or not. This method is safe because it does not throw an exception if the conversion fails.
Syntax-
Using Convert.ToInt32
The Convert.ToInt32 method converts the specified string position of any number to a 32-bit equivalent signed integer. It handles null values by returning 0 and throws a FormatException if the string is not a valid integer.
Syntax-
Handling exceptions and edge cases
When using int.Parse or Convert.ToInt32, you must deal with potential exceptions to avoid runtime errors.
Syntax-
int.tryParsebecause it returns the boolean value true or false.Example-
The below example illustrate the different way to convert string into int in C#
Output-
Also, Read: What is Boxing and Unboxing in C#?