Can anyone tell me any good reason why the C# compiler will not allow
casting an int to a bool?

0) Because a bool is not an int (and a System.Boolean is not a
System.Int32 etc...)
1) To resolve the ambiguity about what value true is (e.g. -1,
non-zero, whatever).
2) To prevent stupidity like the old C/C++ statement if (x = 2) for
having a chance of creeping into the language.

bool x = (bool)1;

If you want to make the call about what true is, YOU MAKE IT, and thus
the code is explicit.

int i = 1;
bool x = (i != 0);  // or (i == 1) or (i == -1)...

--
"You hide yourself with sanctimony, I hide myself with narcissism."
--T-Bone Burnett

Marc C. Brooks
http://musingmarc.blogspot.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to