为什么Boolean.TryParse()]和Convert.ToBoolean()以不同的方式计算字符串?(Why do Boolean.TryParse()] and Convert.ToBool

编程入门 行业动态 更新时间:2024-10-27 11:23:13
为什么Boolean.TryParse()]和Convert.ToBoolean()以不同的方式计算字符串?(Why do Boolean.TryParse()] and Convert.ToBoolean() evaluate a string differently? [duplicate])

这个问题在这里已有答案:

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 answers

Why 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.

更多推荐

本文发布于:2023-08-03 17:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   方式   ToBoolean   Convert   Boolean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!