In C#, we make use of the Convert.ToInt32(bool)
method to convert a boolean value to a 32-bit signed integer. 32-bit signed integers or Int32
values range from -2,147,483,648
to 2,147,483,647
.
Convert.ToInt32(booleanValue)
booleanValue
: This is the boolean value that we want to convert to an Int32
value.
An Int32
equivalent of the boolean value gets returned.
using System;using System.Text;namespace BoolToInt32{class Program{static void Main(string[] args){// create some boolean valuesbool bool1 = true;bool bool2 = false;// convert Boolean values to Int32 valuesConsole.WriteLine("Convert.ToInt32(bool1): " + Convert.ToInt32(bool1));Console.WriteLine("Convert.ToInt32(bool2): " + Convert.ToInt32(bool2));Console.WriteLine("Convert.ToInt32(false): " + Convert.ToInt32(false));Console.WriteLine("Convert.ToInt32(true): " + Convert.ToInt32(true));}}}
In the code above:
Convert.Int32()
method, we convert the boolean values to Int32
values. Then we print the results to the console.