How can I check a user input string to see if it is in numeric form (so I can then parse it to an integer) in c#?
Asked
Active
Viewed 167 times
1 Answers
1
Use Int.TryParse(). It returns a boolean that indicates if the string is a valid integer, and the parsed value as an out param.
http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}

TGH
- 38,769
- 12
- 102
- 135