This post will discuss how to convert bool to int in C#. The boolean value true is represented by 1, and false by 0.

1. Using Convert.ToInt32() method

Unlike C++, C# doesn’t support implicit conversion from type bool to int. The Convert.ToInt32() converts the specified value to the equivalent 32-bit signed integer. It is overloaded for all data types, including Boolean, and returns the integer 1 if the specified value is true; otherwise, 0.

The following example converts the boolean value true to 1 and boolean value false to 0 using Convert.ToInt32().

Download  Run Code

2. Using Custom method

We can also explicitly create an extension method to convert a boolean value to an integer. The following example provides an illustration.

Download  Run Code

That’s all about converting bool to int in C#.