[ACCEPTED]-Convert string to int and test success in C#-int

Accepted answer
Score: 77

Int32.TryParse(String, Int32) - http://msdn.microsoft.com/en-us/library/f02979c7.aspx

  bool result = Int32.TryParse(value, out number);
  if (result)
     Console.WriteLine("Converted '{0}' to {1}.", value, number);         


Score: 13

Could you not make it a little more elegant 2 by running the tryparse right into the if?

Like 1 so:

if (Int32.TryParse(value, out number))     
  Console.WriteLine("Converted '{0}' to {1}.", value, number);
Score: 5



Score: 2

found this in one of the search results: How do I identify if a string is a number?

Adding 5 this because the answers i saw before did 4 not have usage:

int n;
bool isNumeric = int.TryParse("123", out n);

here "123" can be something like 3 string s = "123" that the OP is testing and the value 2 n will have a value (123) after the call if 1 it is found to be numeric.

More Related questions