简体   繁体   English

c#:tryparse vs convert

[英]c#: tryparse vs convert

Today I read an article where it's written that we should always use TryParse(string, out MMM ) for conversion rather than Convert.ToMMM(). 今天我读了一篇文章,其中写道我们应该总是使用TryParse(字符串,输出MMM)进行转换而不是Convert.ToMMM()。

I agree with article but after that I got stuck in one scenario? 我同意文章,但之后我陷入了一个场景?

When there will always be some valid value for the string and hence we can also use Convert.ToMMM() because we don't get any exception from Covert.ToMMM(). 当字符串总是有一些有效值时,我们也可以使用Convert.ToMMM(),因为我们没有从Covert.ToMMM()中获得任何异常。

What I would like to know here is: is there any performance impact when we use TryParse because when I know that the out parameter is always going to be valid then we can use Convert.ToMMM() rather TryParse(string, out MMM) 我想知道的是:当我们使用TryParse时是否有任何性能影响因为当我知道out参数总是有效时我们可以使用Convert.ToMMM()而不是TryParse(字符串,输出MMM)

What do you think? 你怎么看?

If you know the value can be coverted, just use Parse() . 如果你知道值可以被转换,只需使用Parse() If you 'know' that it can be converted, and it can't, then an exception being thrown is a good thing. 如果你“知道”它可以被转换,而且它不能被转换,那么引发异常是一件好事。

EDIT: Note, this is in comparison to using TryParse or Convert without error checking. 编辑:注意,这与使用TryParseConvert而不进行错误检查相比。 If you use either of the other methods with proper error checking then the point is moot. 如果您使用其他任何一种方法进行适当的错误检查,那么这一点就没有实际意义。 I'm just worried about your assumption that you know the value can be converted. 我只是担心你的假设,你知道价值可以转换。 If you want to skip the error checking, use Parse and die immediately on failure rather than possibly continuing and corrupting data. 如果您想跳过错误检查,请使用Parse并在发生故障时立即死亡,而不是继续并破坏数据。

When the input to TryParse / Convert.ToXXX comes from user input, I'd always use TryParse . TryParse / Convert.ToXXX的输入来自用户输入时,我总是使用TryParse In case of database values, I'd check why you get string s from the database (maybe bad design?). 在数据库值的情况下,我会检查你从数据库中获取string的原因(可能是糟糕的设计?)。 If string values can be entered in the database columns, I'd also use TryParse as you can never be sure that nobody modifies data manually. 如果可以在数据库列中输入字符串值,我也会使用TryParse因为您永远无法确定没有人手动修改数据。

EDIT 编辑
Reading Matthew's reply: If you are unsure and would wrap the conversion in a try-catch-block anyway, you might consider using TryParse as it is said to be way faster than doing a try-catch in that case. 阅读Matthew的回复:如果您不确定并且无论如何都会将转换包装在try-catch-block中,您可以考虑使用TryParse ,因为据说它比在这种情况下尝试catch更快。

There is significant difference regarding the developing approach you use. 您使用的开发方法存在显着差异。

Convert : Converting one "primitive" data in to another type and corresponding format using multiple options 转换 :使用多个选项将一个“原始”数据转换为另一种类型和相应的格式
Case and point - converting an integer number in to its bit by bit representation . 大小写和点 - 将整数转换为逐位表示 Or hexadecimal number (as string) in to integer, etc... 或十六进制数(作为字符串)到整数等...
Error Messages : Conversion Specific Error Message - for problems in multiple cases and at multiple stages of the conversion process. 错误消息 :特定于转换的错误消息 - 针对多个案例以及转换过程的多个阶段的问题。

TryParse: Error-less transfer from one data format to another. TryParse:从一种数据格式到另一种数据格式的无错传输。 Enabling T/F control of possible or not. 是否可以启用T / F控制。
Error Messages: NONE 错误消息:
NB: Even after passing the data in to a variable - the data passed is the default of the type we try to parse in to. 注意:即使将数据传递给变量 - 传递的数据也是我们尝试解析的类型的默认值

Parse : in essence taking some data in one format and transfer it in to another. 解析 :实质上以一种格式获取一些数据并将其传输到另一种格式。 No representations and nothing fancy. 没有任何陈述,也没有任何幻想。
Error Messages: Format-oriented 错误消息:面向格式

PS Correct me if I missed something or did not explain it well enough. PS纠正我,如果我错过了一些东西或者说不够好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM