[ACCEPTED]-Convert string to int and test success in C#-int
Accepted answer
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);
}
0
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);
Int.TryParse
0
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.