How can i avoid cast numbers to booleans in C#?

116 views Asked by At

I am writing a component for grasshopper in c#. But went i pass a number through a boolean it give me a a false or a true value. How can i add a exception to this event. I only want to pass as parameter booleans types

2

There are 2 answers

0
rabelloo On

Boolean types in C# can only have true or false values. If you wish to pass a number as a parameter, define it as int.

1
Caverna On

Wihtout any code is hard to tell, but if your method must have int in its signature, you can "translate" to result = value != 0. But why don't you just modify your signature to bool anyway? If you want to, you may have both!

void method(int value) {
   bool result = value != 0;
   method(result);
}
void method(bool value) {
    // your code
}