Svetlozar Angelov
Svetlozar Angelov

Reputation: 21680

C# can't cast bool to int

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?

Upvotes: 44

Views: 66157

Answers (10)

GlinoMessi
GlinoMessi

Reputation: 1

var n = (bool)your_int ? 1 : 0

Upvotes: 0

Ahad aghapour
Ahad aghapour

Reputation: 2553

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

public static int ToInt32(bool value);

Upvotes: 5

Delerium
Delerium

Reputation: 5

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

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

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

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.

Upvotes: -2

Kai
Kai

Reputation: 5910

int n = (bBool) ? 1 : 0

Upvotes: -1

kemiller2002
kemiller2002

Reputation: 115548

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

if (5 > 0) is easier to understand rather than 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.

Upvotes: 30

Noldorin
Noldorin

Reputation: 147471

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

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.

Upvotes: 20

tvanfosson
tvanfosson

Reputation: 532765

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.

For example,

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

won't even compile in C#, but it will both compile and execute incorrectly in 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.

Upvotes: 5

Joey
Joey

Reputation: 354874

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:

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 int is intended as int or bool).

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

Convert.ToInt32(someBool)

as Noldorin mentioned or roll your own with

someBool ? 1 : 0

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

Upvotes: 25

Hardryv
Hardryv

Reputation: 793

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

Upvotes: 3

Peter Drier
Peter Drier

Reputation: 1417

number != 0, is too prone to error.

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

Upvotes: -3

Related Questions