Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no String.IsNumeric Function in C#

Tags:

c#

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.

like image 663
pradeeptp Avatar asked Oct 02 '09 09:10

pradeeptp


People also ask

How do you check if a string is a digit in C?

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.

How do you use IsNumeric function?

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.


1 Answers

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.

like image 143
Mark Heath Avatar answered Oct 06 '22 01:10

Mark Heath