C# Convert String to Number


Try here for an article on converting a string to an integer. The best solution is to use TryParse and if the string is not a valid integer, then return a nullable integer. The suggested code at that website is below.

private int? ConvertStringToInt(string intString)
{
   int i=0;
   return (Int32.TryParse(intString, out i) ? i : (int?)null);
}

There are lots of times you need to convert a string to a number. For example, if you are asking a user to enter a number and you need to perform calculations on it, you will need to convert the string to a number and ensure that it is a valid number.