How to convert a boolean to an Int32 in C#

Overview

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.

Syntax

Convert.ToInt32(booleanValue)
Syntax for converting a boolean value to Int32 in C#

Parameters

booleanValue: This is the boolean value that we want to convert to an Int32 value.

Return value

An Int32 equivalent of the boolean value gets returned.

Example

using System;
using System.Text;
namespace BoolToInt32
{
class Program
{
static void Main(string[] args)
{
// create some boolean values
bool bool1 = true;
bool bool2 = false;
// convert Boolean values to Int32 values
Console.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));
}
}
}

Explanation

In the code above:

  • Lines 11–12: We create some boolean values.
  • Lines 15–18: With the Convert.Int32() method, we convert the boolean values to Int32 values. Then we print the results to the console.

Free Resources