Convert.ToBoolean和Boolean.Parse不接受0和1不接受、ToBoolean、Convert、Parse

2023-09-04 06:58:02 作者:帅的好烦躁

为什么会决定,解析一个布尔值时,0/1是不能接受的?

Why was it decided that when parsing a boolean, 0/1 are not acceptable?

在分析任何整型值,它接受数字字符串进行解析。 (如果.NET可以解析字符串一亿265865我会感到惊讶)。

When parsing any integer type value, it accepts numerical strings to be parsed. (And if .NET can parse the string "One hundred million two hundred and sixty five thousand eight hundred and sixty five" I would be surprised).

是什么让布尔特别?他们基本上是0假,非零为真以我的经验...

What makes booleans special? They are essentially 0 as false, and non-zero as true in my experience...

有一个BCL方法来分析这样的字符串,如果没有,为什么?

Is there a bcl method to parse a string like this, and if not, why?

注:I忘了指定字符串中0和1。好奇的是,如果已经是工程,我期待一个int。也许这造成了混乱。

Note: I forgot to specify in a string "0" and "1". Curious though that if already an int it works as I anticipated. Maybe this caused the confusion.

推荐答案

0和(非归零)不等于假和真,他们只是由C所选择的重新presentation其他语言使用0为真,-1表示假,或其他方案完全。一个布尔是不可以 0或1,这是一个真或假。

0 and (not-zero) are not equal to "false" and "true", they're just the representation chosen by C. Other languages use 0 for true and -1 for false, or other schemes entirely. A boolean is not a 0 or a 1, it's a true or a false.

如果它还要处理是和否,关闭和关于,和所有的类似于布尔无数其他的东西?你会在哪里划清界线呢?

Should it also handle "yes" and "no", "off" and "on", and all of the myriad other things that are analogous to booleans? Where would you draw the line?