
You need to convert your bool value to an int, 0 or 1. In other languages, false is equivalent to 0 and true is equivalent to 1, but this is not possible in the C# language. Here we look at ways you can convert bools to ints, first running through an example and then looking at the alternatives.
First, you cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. It is the same rule that mandates you cannot test an integer in an if statement. Here we convert from bool to int correctly.
This C# example program converts bool values to int values. It shows several ways to do this.
Program that uses bools [C#]
using System;
class Program
{
static void Main()
{
// Example bool is true
bool t = true;
// A
// Convert bool to int
int i = t ? 1 : 0;
Console.WriteLine(i); // 1
// Example bool is false
bool f = false;
// B
// Convert bool to int
int y = Convert.ToInt32(f);
Console.WriteLine(y); // 0
}
}
Output
1
0Description. When I used this code, I felt there had to be some way to turn the true into a 1, and the false into a 0. However, this is not possible according to my research.
Error 1: Cannot convert type 'bool' to 'int'

Casting problems. You cannot cast bool to int, such as in the statement (int)true, without getting the compiler error shown above. Opening up Convert.ToInt32 up in IL Disassembler, I found it simply tests the bool parameter against true and returns 1 if it is true, or false otherwise.
MSDN referenceCompilation note. Therefore, A and B above are compiled into the same basic code. In my further investigation, I benchmarked the two statements and found identical performance. The compiler efficiently inlines Convert.ToInt32(bool) to be the same as the ternary expression in A. Therefore, A and B follow the same instructions. There is more information about the ternary operator on this site.
Ternary Operator Use
Here we saw that you must use a ternary or if statement to convert from bool to int. I suggest that the ternary statement above is best, as it involves the fewest characters to type and is simple. Extension methods could solve this problem partly, but they would make most projects more complex than just using the ternary statement.
Convert Int to Bool Cast Examples