As several others have mentioned, you can also use int.Parse() and int.TryParse().
If you're certain that the string will always be an int:
int myInt = int.Parse(myString);
If you'd like to check whether string is really an int first:
int myInt;
bool isValid = int.TryParse(myString, out myInt); // the out keyword allows the method to essentially "return" a second value
if (isValid)
{
int plusOne = myInt + 1;
}
int i;
string whatever;
//Best since no exception raised
int.TryParse(whatever, out i);
//Better use try catch on this one
i = Convert.ToInt32(whatever);
string varString = "15";
int i = int.Parse(varString);
or
int varI;
string varString = "15";
int.TryParse(varString, out varI);
int.TryParse is safer since if you put something else in varString (for example "fsfdsfs") you would get an exception. By using int.TryParse when string can't be converted into int it will return 0.
y = 0;
for (int i = 0; i < s.Length; i++)
y = y * 10 + (s[i] - '0');
"s" is your string that you want converted to an int. This code assumes you won't have any exceptions during the conversion. So if you know your string data will always be some sort of int value, the above code is the best way to go for pure speed.
bool result = Int32.TryParse(someString, out someNumeric)
This method will try to convert someString into someNumeric, and return a result depending on whether or not the conversion is successful: true if conversion is successful and false if conversion failed. Take note that this method will not throw an exception if the conversion failed like how Int32.Parse method did and instead returns zero for someNumeric.