-2

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#?

MirrorEyes
  • 57
  • 4

1 Answers1

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