To determine whether a string is a valid representation of a specified numeric type, use the staticTryParse method that is implemented by all primitive numeric types and also by types such asDateTime andIPAddress. The following example shows how to determine whether "108" is a validint.
Primitive numeric types also implement the Parse static method, which throws an exception if the string is not a valid number.TryParse is generally more efficient because it just returns false if the number is not valid.
Always use the TryParse or Parse methods to validate user input from controls such as text boxes and combo boxes.
本文介绍了如何使用TryParse方法来高效地验证一个字符串是否为有效的数值类型表示。此方法不仅适用于基本的数值类型,还适用于如DateTime和IPAddress等类型。相较于抛出异常的Parse方法,TryParse提供了一种更优的性能选择。
503

被折叠的 条评论
为什么被折叠?



