哪个更好: int.TryParse 或 try { int.Parse() } catch

发布于 2024-10-16 14:39:27 字数 1432 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

俯瞰星空 2024-10-23 14:39:28

首先,到目前为止。正如乔治所说,第二个是异常编码,它对性能有重大影响。性能始终是一个值得关注的问题。

First, by far. As George said, second is coding by exception and has major performance impacts. And performance should be a concern, always.

热血少△年 2024-10-23 14:39:28

捕获异常会产生更多开销,因此我将选择 TryParse。

if (int.TryParse(string, out num))

另外,如果转换失败,TryParse 方法 不会引发异常。它消除了在转换无效且无法成功解析的情况下使用异常处理来测试 FormatException 的需要。

Catching an Exception has more overhead, so I'll go for TryParse.

if (int.TryParse(string, out num))

Plus, the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that the conversion is invalid and cannot be successfully parsed.

神经暖 2024-10-23 14:39:28

另一件需要记住的事情是,异常会记录在 Visual Studio 调试/输出窗口中(可选)。即使异常的性能开销可能微不足道,在调试时为每个异常编写一行文本也会减慢速度。更值得注意的异常也可能被淹没在失败的整数解析操作的所有噪音中。

Something else to keep in mind is that exceptions are logged (optionally) in the Visual Studio debug/output window. Even when the performance overhead of exceptions might be insignificant, writing a line of text for each exception when debugging can slow things right down. More noteworthy exceptions might be drowned amongst all the noise of failed integer parsing operations, too.

尐籹人 2024-10-23 14:39:27

更好是非常主观的。例如,我个人更喜欢 int.TryParse,因为我通常不关心解析失败的原因。但是,int.Parse 可以(根据文档< /a>) 抛出三个不同的异常:

  • 输入为 null
  • 输入的格式无效
  • 输入包含会产生溢出的数字

如果您关心它失败的原因,那么 int.Parse 是显然是更好的选择。

一如既往,上下文为王。

Better is highly subjective. For instance, I personally prefer int.TryParse, since I most often don't care why the parsing fails, if it fails. However, int.Parse can (according to the documentation) throw three different exceptions:

  • the input is null
  • the input is not in a valid format
  • the input contains a number that produces an overflow

If you care about why it fails, then int.Parse is clearly the better choice.

As always, context is king.

黎歌 2024-10-23 14:39:27

转换有时会失败是例外,还是转换有时会失败预期且正常?如果是前者,请使用异常。如果是后者,避免异常。异常被称为“异常”是有原因的;您应该只使用它们来处理特殊情况。

Is it exceptional for the conversion to sometimes fail, or is it expected and normal that the conversion will sometimes fail? If the former, use an exception. If the latter, avoid exceptions. Exceptions are called "exceptions" for a reason; you should only use them to handle exceptional circumstances.

念三年u 2024-10-23 14:39:27

如果确实预计转换有时会失败,我喜欢使用 int.TryParse 等与 条件(三元)运算符,如下所示:

int myInt = int.TryParse(myString, out myInt) ? myInt : 0;

在这种情况下,如果 TryParse 则将使用零作为默认值方法失败。

对于可空类型也非常有用,如果转换失败,它将用 null 覆盖任何默认值。

If it is indeed expected that the conversion will sometimes fail, I like to use int.TryParse and such neatly on one line with the conditional (Ternary) operator, like this:

int myInt = int.TryParse(myString, out myInt) ? myInt : 0;

In this case zero will be used as a default value if the TryParse method fails.

Also really useful for nullable types, which will overwrite any default value with null if the conversion fails.

冰雪之触 2024-10-23 14:39:27

第一个。第二个被认为是异常编码

The first. The second is considered coding by exception.

望她远 2024-10-23 14:39:27

就我个人而言,我更喜欢:

if (int.TryParse(string, out num))
{
   ...
} 

Personally, I'd prefer:

if (int.TryParse(string, out num))
{
   ...
} 
笑看君怀她人 2024-10-23 14:39:27

第一个!您不应该通过异常进行编码。

您可以将其缩短为

if (int.TryParse(string, out num))

The first! You should not code by exception.

you could shorten it to

if (int.TryParse(string, out num))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文