解析诉的TryParse [英] Parse v. TryParse

查看:105
本文介绍了解析诉的TryParse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解析()和的TryParse()?

What is the difference between Parse() and TryParse()?

int number = int.Parse(textBoxNumber.Text);

// The Try-Parse Method
int.TryParse(textBoxNumber.Text, out number);

是否有某种形式的错误检查就像一个try-catch块?

Is there some form of error-checking like a Try-Catch Block?

推荐答案

解析抛出一个异常,如果它不能解析的值,而的TryParse 返回布尔表示是否成功。

Parse throws an exception if it cannot parse the value, whereas TryParse returns a bool indicating whether it succeeded.

的TryParse 不只是尝试 / 内部 - 它的整点是,它是没有例外,使其快速实现。事实上,它是最有可能实现的方式是内部的解析方法将调用的TryParse ,然后抛出,如果一个异常它返回

TryParse does not just try/catch internally - the whole point of it is that it is implemented without exceptions so that it is fast. In fact the way it is most likely implemented is that internally the Parse method will call TryParse and then throw an exception if it returns false.

在简单地说,使用解析如果您确定值将是有效的;否则使用的TryParse

In a nutshell, use Parse if you are sure the value will be valid; otherwise use TryParse.

这篇关于解析诉的TryParse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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