Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
bool
integer
int32
communitycreator

How to convert a boolean to an Int32 in C#

Theodore Kelechukwu Onyejiaku

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));
        }
    }
}
Convert boolean values to Int32 values

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.

RELATED TAGS

c#
bool
integer
int32
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring