I need to check if an input is an int or not. Is there a similar function to the String.IsNullOrEmpty(), like an Int.isWholeNumber() function?
Is there a way to validate this inside the if()
statement only, without having to declare an int before? (As you need to do with TryParse())
EDIT
I need to validate an area code (five numbers)
I don't believe there's any such method within the BCL, but it's easy to write one (I'm assuming you're really talking about whether a string can be parsed as an integer):
public static bool CanParse(string text)
{
int ignored;
return int.TryParse(text, out ignored);
}
Add overloads accepting IFormatProvider
values etc as you require them. Assuming this is for validation, you might want to expand it to allow a range of valid values to be specified as well...
If you're doing a lot of custom validation, you may well want to look at the Fluent Validation project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With