
int.Parse converts strings into ints. Often we have strings containing char digits. After converting them into integers, we can use numeric values. There are several C# methods, including int.Parse, that are ideal for this.
String input:
"500"
"-5"
"xyz"
"0.5"
Parsed number:
500
-5
Error
Error

First, here we see the int.Parse method. int.Parse is the simplest method, and is also my favorite for many situations. It throws exceptions on invalid input, which can be slow if they are common. It does not contain any internal null checks.
These C# examples demonstrate the int.Parse method. They convert strings into ints.
Example program that uses int.Parse [C#]
using System;
class Program
{
static void Main()
{
// Convert string to number.
string text = "500";
int num = int.Parse(text);
Console.WriteLine(num);
}
}
Output
500
One of the most useful methods for parsing integers in the C# language is the int.TryParse method. This method works the same way as int.Parse as shown above, but it uses somewhat more confusing syntax and does not throw exceptions. You must describe the second parameter with the out modifier, and TryParse also returns true or false based on its success.
int.TryParseLet's look at the Convert.ToInt32 method. Convert.ToInt32, along with its siblings Convert.ToInt16 and Convert.ToInt64, is actually a static wrapper method for the int.Parse method. It can be slower than int.Parse if the surrounding code is equivalent.
Convert.ToInt32Program that uses Convert.ToInt32 [C#]
using System;
class Program
{
static void Main()
{
// Convert 'text' string to an integer with Convert.ToInt32.
string text = "500";
int num = Convert.ToInt32(text);
Console.WriteLine(num);
}
}
Output
500
The syntax for this method may be more confusing as it uses the bit size of the int, which may not be really important for some of your code. The .NET Framework can better deal with that sort of detail.
Note: These Convert methods don't seem to add any value here, and may be harder to read for some. It is probably better to focus more on the faster and simpler functions.

My recommendation is to use int.Parse when your input will be valid, as it makes for simpler calling code. It isn't always perfect, but it is a winner. On the other hand, use int.TryParse when you will be dealing with corrupt data.
int.Parse OptimizationTip 1:
If your input contains non-numeric or invalid characters, use int.TryParse.
Tip 2:
If your input is valid and all numeric, use int.Parse.
Tip 3:
If your input is guaranteed to be valid,
use custom code if performance is critical.
Otherwise, use int.Parse.
Tip 4:
If your input has many digits or decimal places use double.Parse or double.TryParse.

You can see a more detailed and complete example of testing strings to see if they are numbers in another article on this site. The technique involves using int.TryParse or similar methods in a static Boolean method. This is useful for validating input on the server or in TextBox controls.
String Numbers
We saw how you can parse integers in the C# language very easily with three different methods. Before frameworks were widely used, programmers would write their own integer conversion routines, but this quickly became very complex. The .NET Framework provides much more built-in functionality.
String Type