这个问题在这里已有答案:
Convert.ToBoolean(string)和Boolean.Parse(string)有什么区别? 3个答案为什么Boolean.TryParse()和Convert.ToBoolean()以不同的方式计算字符串?
我理解他们最终会以不同方式进行评估:
Boolean.TryParse()将匹配(不区分大小写)'true'和'false'。 Convert.ToBoolean()将匹配整个范围的值(在上面链接的Microsoft doco中演示的示例),我认为这更自然。它是我不明白的差异背后的原因。
有几个涉及这个问题的讨论似乎没有解决这个特定的问题。
This question already has an answer here:
What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)? 3 answersWhy do Boolean.TryParse() and Convert.ToBoolean() evaluate a string differently?
I understand how they end up evaluating differently:
Boolean.TryParse() will match (case insensitive) 'true' and 'false'. Convert.ToBoolean() will match to a whole range of values (example demonstrated in Microsoft doco linked above) which I would consider more natural.Its the reasoning behind the difference I dont understand.
There are a couple of discussions touching on this subject which don't seem to address this particular question.
最满意答案
它在方法/类名中。
转换 - >你已经有了一些值,你convert它convert为另一种类型。 例如,您有值1可以转换为true 。
解析 - >您将值作为字符串并parse它。
It's in the method/class names.
Convert -> you already have some value, you convert it to another type. e.g. you have value 1 which can be converted to true.
Parse -> you have the value as a string and you parse it.
更多推荐
发布评论