C# 不能将 bool 转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/946688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:59:22  来源:igfitidea点击:

C# can't cast bool to int

c#.net

提问by Svetlozar Angelov

We all know that in C# we can't cast bool to int. I wanted to see what is the binary representation of true with bitmask, but I can't use (bool & int).. I think the problem is the architecture desicion "true is true, not any number != 0" (C++) and I was wondering what the benefits of such an architecture are? What is so bad with the C true/false concept?

我们都知道在 C# 中我们不能将 bool 转换为 int。我想看看带位掩码的 true 的二进制表示是什么,但我不能使用 (bool & int)。我想知道这样的架构有什么好处?C 真/假概念有什么不好?

采纳答案by kemiller2002

It's clearer to the programmer when an integer can't be used for trueor false.

当整数不能用于trueor时,程序员会更清楚false

if (5 > 0)is easier to understand rather than if(5)

if (5 > 0)更容易理解而不是 if(5)

It's the same reason why they don't allow fall through conditions in switch statements. It's too easy to make a mistake.

这与他们不允许在 switch 语句中的条件失败的原因相同。太容易犯错了。

回答by Noldorin

Just for information, Convert.ToInt32(fooBool)should tell you that trueis represented by 1, and falseby 0. (This is an arbitrary representation in the BCL, and not necessarily the one used it memory, however.)

仅供参考,Convert.ToInt32(fooBool)应该告诉您它true由 1 和false0 表示。(这是 BCL 中的任意表示,但不一定是使用它的内存。)

The point here is, the bit representation really ought to be meaningless to any programmer. Booleans are meant to be used for specific purposes (i.e. flags), and shouldn't be mixed with ints, else the uses of variables can potentially become quite confusing.

这里的重点是,位表示对任何程序员来说都应该毫无意义。布尔值旨在用于特定目的(即标志),不应与整数混合使用,否则变量的使用可能会变得非常混乱。

回答by Peter Drier

number != 0, is too prone to error.

number != 0,太容易出错。

C# made a lot of design decisions to subvert common mistakes like this can lead to.

C# 做出了很多设计决策来颠覆像这样可能导致的常见错误。

回答by Hardryv

Nobody likes all of any other entity's decisions... just write your own converter (quick-&-dirty-like)

没有人喜欢任何其他实体的所有决定......只需编写自己的转换器(quick-&-dirty-like)

回答by Joey

In C integers are frequently doubly used as a normal number and a boolean, such as for loops that run while a certain number is not zero. That's a use which clearly has its merits for concise code, like a na?ve strlen routine:

在 C 中,整数经常被双重用作普通数字和布尔值,例如在某个数字不为零时运行的 for 循环。这种用法对于简洁的代码显然有其优点,就像一个天真的 strlen 例程:

const char *s;
for (s = str; *s; ++s)
    ;
return (s - str);

but while short it masks the true purpose of that snippet, which essentially says "loop while the character I am looking at is not the null character". But written down it just says "treat it as a boolean when I feel like it, and as a number in some other cases".

但是虽然简短,但它掩盖了该片段的真正目的,它本质上说“循环而我正在查看的字符不是空字符”。但是写下来只是说“当我喜欢它时将其视为布尔值,而在其他情况下则将其视为数字”。

This kind of double nature allegedly frequently leads to problems and it makes the code less readable (since you have to judge from the context whether a given usage of an intis intended as intor bool).

据称,这种双重性质经常会导致问题,并且会降低代码的可读性(因为您必须从上下文中判断 an 的给定用法int是否旨在作为intbool)。

If you absolutely need to use a boolean as an integer you can either use

如果您绝对需要使用布尔值作为整数,您可以使用

Convert.ToInt32(someBool)

as Noldorin mentionedor roll your own with

正如诺多林提到的或自己动手

someBool ? 1 : 0

Both of which clearly say that you are using an int(and not a bool).

两者都清楚地表明您正在使用一个int(而不是一个bool)。

回答by tvanfosson

I don't think the issue is some much about true/false or 1/0 as it is about the decision to make C# a strongly-typed language rather than a weakly-typed language. There are many benefits (and some drawbacks) to a language being strongly typed. One of the benefits is that it reduces bugs due to misuse (or incorrect use) of expressions.

我认为这个问题与真/假或 1/0 无关,因为它是关于使 C# 成为强类型语言而不是弱类型语言的决定。强类型语言有很多好处(也有一些缺点)。好处之一是它减少了由于滥用(或不正确使用)表达式而导致的错误。

For example,

例如,

int i = 0;
if (i = 1) {
   ...
}

won't even compile in C#, but it will both compile and execute incorrectly in C.

甚至不会在 C# 中编译,但它会在 C 中编译和执行不正确。

Choosing to make C# a strongly-typed language infers these benefits to C# programmers. Even so, they could have introduced a conversion from bool to int (and back) if they choose. I suspect that they didn't do so due to the potential for bugs like that above to be introduced.

选择使 C# 成为强类型语言可以为 C# 程序员带来这些好处。即便如此,如果他们愿意,他们也可以引入从 bool 到 int(并返回)的转换。我怀疑他们没有这样做,因为可能会引入上述错误。

回答by Kai

int n = (bBool) ? 1 : 0

int n = (bBool) ? 1 : 0

回答by Delerium

You make a good point on these. However it does remove one shortcut.

你在这些方面提出了很好的观点。但是它确实删除了一个快捷方式。

With c, toggles can be easily done by the code "bBool = 1 - bBool".

使用 c,可以通过代码“bBool = 1 - bBool”轻松完成切换。

If bBool is true (1), then 1 - 1 = 0. bBool changes to false.

如果 bBool 为真 (1),则 1 - 1 = 0。bBool 变为假。

If bBool is false (0), then 1 - 0 = 1. bBool changes to true.

如果 bBool 为假 (0),则 1 - 0 = 1。bBool 变为真。

Instead in C# I'm required to do a conditional to check the state, and then set it to it's opposite, which causes more work for the machine and me.

相反,在 C# 中,我需要做一个条件来检查状态,然后将其设置为相反,这会给机器和我带来更多的工作。

回答by Ahad aghapour

we can use Convert.ToInt32, its definition is here:

我们可以使用 Convert.ToInt32,它的定义在这里:

public static int ToInt32(bool value);