为什么会出现在C#中没有String.IsNumeric功能 [英] Why is there no String.IsNumeric Function in C#

查看:314
本文介绍了为什么会出现在C#中没有String.IsNumeric功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇,想知道为什么C#语言的团队还没有String对象上提供的IsNumeric函数? 。IsNumeric函数将是一个String对象的Int32.TryParse功能上更恰当。



请注意:我问这个问题,因为我发现它很难解释这初学者,我不喜欢他们咒骂C#这个!



更新:拜托,这个问题不是关于如何实现一个IsNumeric函数


解决方案<。 / DIV>

通过这样的方法将被作为确定数字真正重要的麻烦。如果它允许小数点,是否应该允许前导空格,它应该有一个上限的位数?



Int32.TryParse回答更加明确定义的非常重要的问题的,这是一个Int32的有效字符串表示?



当然,没有什么是阻止你编写自己的扩展方法,即决定一个字符串是否是数字根据自己的规则。


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.

解决方案

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.

这篇关于为什么会出现在C#中没有String.IsNumeric功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆