Registered User wrote:
On Fri, 21 Sep 2007 21:39:26 -0700, John <no@spam.comwrote:
>The following code:
int test = 1;
bool isTrue = (bool)test;
results in a compiler error:
Cannot convert type 'int' to 'bool'
wtf, any ideas on how to work around this?
bool isTrue = Convert.ToBoolean(test);
You will find that non-zero integer arguments to Convert.ToBoolean all
return true.
regards
A.G.
The Convert.ToBoolean(int) method is implemented as:
public static bool ToBoolean(int value) {
return (value != 0);
}
So it's the same as doing the operation yourself:
bool isTrue = (test != 0);
Perhaps the compiler manages to inline the method call, so that the
generated code is the same in both cases. Either way the difference in
performance is normally negligable, so you should just go with the one
that best describes why you are doing the conversion.
:)
--
Göran Andersson
_____
http://www.guffa.com