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.
另一件需要记住的事情是,异常会记录在 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.
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.
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.
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.
发布评论
评论(9)
首先,到目前为止。正如乔治所说,第二个是异常编码,它对性能有重大影响。性能始终是一个值得关注的问题。
First, by far. As George said, second is coding by exception and has major performance impacts. And performance should be a concern, always.
捕获异常会产生更多开销,因此我将选择 TryParse。
另外,如果转换失败,TryParse 方法 不会引发异常。它消除了在转换无效且无法成功解析的情况下使用异常处理来测试 FormatException 的需要。
Catching an Exception has more overhead, so I'll go for TryParse.
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.
另一件需要记住的事情是,异常会记录在 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.
更好是非常主观的。例如,我个人更喜欢
int.TryParse
,因为我通常不关心解析失败的原因。但是,int.Parse
可以(根据文档< /a>) 抛出三个不同的异常:如果您关心它失败的原因,那么
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:If you care about why it fails, then
int.Parse
is clearly the better choice.As always, context is king.
转换有时会失败是例外,还是转换有时会失败预期且正常?如果是前者,请使用异常。如果是后者,避免异常。异常被称为“异常”是有原因的;您应该只使用它们来处理特殊情况。
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.
如果确实预计转换有时会失败,我喜欢使用
int.TryParse
等与 条件(三元)运算符,如下所示:在这种情况下,如果 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: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.第一个。第二个被认为是异常编码。
The first. The second is considered coding by exception.
就我个人而言,我更喜欢:
Personally, I'd prefer:
第一个!您不应该通过异常进行编码。
您可以将其缩短为
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))