I am just curious to know why the C# language team has not provided a IsNumeric function on the String object?. IsNumeric function would have been more proper on a String object that the Int32.TryParse Function.
Note: I am asking this question because I have found it difficult to explain this to a beginner and I don't like them cursing C# for this!
Update: Please, this question is not about how to Implement a IsNumeric function.
Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.
VBA example This example uses the IsNumeric function to determine if a variable can be evaluated as a number. MyVar = "53" ' Assign value. MyCheck = IsNumeric(MyVar) ' Returns True. MyVar = "459.95" ' Assign value.
The trouble with a method like that would be deciding what counts as "numeric". Should it allow decimals points, should it allow leading whitespace, should it have an upper bound for the number of digits?
Int32.TryParse answers the much more well-defined question of, "is this a valid string representation of an Int32?"
And of course, nothing is stopping you writing your own extension method, that decides if a string is numeric according to your own rules.
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