Convert string to int in C#

In C#, you can convert a string to an integer using various methods and techniques. Here are some of the most common approaches with detailed explanations and examples:

Using int.Parse() method

The int.Parse() method is used to convert a string representation of an integer to an actual integer value. This method throws an exception if the conversion fails.

using System; class Program { static void Main() { string strNumber = "42"; int result = int.Parse(strNumber); Console.WriteLine(result); // Output: 42 } }

Using int.TryParse() method

The int.TryParse() method allows you to convert a string to an integer safely without throwing an exception in case of a failed conversion. Instead, it returns a boolean value indicating whether the conversion was successful or not. If successful, the converted integer value is returned via an out parameter.

using System; class Program { static void Main() { string strNumber = "123abc"; // Invalid integer int result; if (int.TryParse(strNumber, out result)) { Console.WriteLine(result); // Output: 123 } else { Console.WriteLine("Invalid input"); } } }

Using Convert.ToInt32() method

The Convert.ToInt32() method is another way to convert a string to an integer. Like int.Parse(), it throws an exception if the conversion fails.

using System; class Program { static void Main() { string strNumber = "9876"; int result = Convert.ToInt32(strNumber); Console.WriteLine(result); // Output: 9876 } }

Using int.Parse() or int.TryParse() with culture-specific formatting

If you are dealing with strings that use different number formats based on the culture, you can use the int.Parse() or int.TryParse() methods with specific CultureInfo settings to handle those cases correctly.

using System; using System.Globalization; class Program { static void Main() { string strNumber = "1,234"; // Number formatted with a comma int result; if (int.TryParse(strNumber, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result)) { Console.WriteLine(result); // Output: 1234 } else { Console.WriteLine("Invalid input"); } } }

Different types of Parse methods


string to int in C#

In C#, there are several Parse methods for different data types. The following are some of the most commonly used Parse methods:


Parse methods Description
int.Parse Converts a string representation of a number to an int.
float.Parse Converts a string representation of a number to a float.
double.Parse Converts a string representation of a number to a double.
decimal.Parse Converts a string representation of a number to a decimal.
bool.Parse Converts a string representation of a boolean value to a bool.
DateTime.Parse Converts a string representation of a date & time to a DateTime.
Guid.Parse Converts a string representation of a globally unique identifier to a Guid value.

Conclusion

These are the main ways to convert a string to an integer in C#. Choose the appropriate method based on your specific use case, and keep in mind the differences in exception handling between int.Parse() and int.TryParse() methods.