Convert Boolean to Integer in C#

Abdullahi Salawudeen Oct 12, 2023
  1. Use the ConvertToInt32 Statement to Convert Boolean to Integer in C#
  2. Use the Ternary Conditional Operator to Convert Boolean to Integer in C#
  3. Use the if Statement to Convert From Boolean to Integer in C#
Convert Boolean to Integer in C#

This article will introduce converting boolean data type to integer in C#.

Use the ConvertToInt32 Statement to Convert Boolean to Integer in C#

Traditionally, there is no implicit conversion of data type from boolean to an integer. However, the Convert.ToInt32() method converts a specified value to a 32-bit signed integer.

It is worthy of mentioning that the Convert.ToInt32() method is similar to the int.Parse() method, but the int.Parse() method only accepts string data type as an argument and throws an error when a null is passed as argument.

In comparison, the Convert.ToInt32() method is not affected by these limitations.

Further discussion is available via this reference.

Below is an example of code using the Convert.ToInt32() method.

// C# program to illustrate the
// use of Convert.ToInt32 statement
// and Convert.ToBoolean
using System;

class Test {
  // Main Method
  static public void Main() {
    bool boolinput = true;
    int intRresult = Convert.ToInt32(boolinput);
    bool boolRresult = Convert.ToBoolean(intRresult);
    Console.Write("When Boolean is True, the converted integer value is: ");
    Console.WriteLine(intRresult);
    Console.Write("When integer is 1, the converted boolean value is: ");
    Console.WriteLine(boolRresult);
  }
}

Output:

When Boolean is True, the converted integer value is: 1
When integer is 1, the converted boolean value is: True

Below is an example of code using the int.Parse() method.

// C# program to illustrate the
// use of int.Parse statement
using System;

class Test {
  // Main Method
  static public void Main() {
    bool boolinput = true;
    int intRresult = int.Parse(boolinput);

    Console.Write("When Boolean is True, the converted integer value is: ");
    Console.WriteLine(intRresult);
    Console.Write("When integer is 1, the converted boolean value is: ");
  }
}

Output:

program.cs(12,30): error CS1503: Argument 1: cannot convert from 'bool' to 'string'

The above error is displayed using the int.Parse method to convert a Boolean data type to integer. It is observed that the method is expecting a string as an argument, not a Boolean data type.

Use the switch Statement to Convert Boolean to Integer in C#

The switch statement is used to conditionally branch during the execution of a program. It decides the code block be executed. The value of the expression is compared with the values of each case,

If there is a match, the associated block of code is executed. The switch expression is evaluated once.

Below is an example of code using the switch statement.

// C# program to illustrate the
// use of switch statement
using System;

namespace Test {
  class Program {
    static void Main(string[] args) {
      int i = 1;
      bool b = true;
      switch (i) {
        case 0:
          b = false;
          Console.WriteLine("When integer is 0, boolean is:");
          Console.WriteLine(b);
          break;
        case 1:
          b = true;
          Console.WriteLine("When integer is 1, boolean is:");
          Console.WriteLine(b);
          break;
      }

      switch (b) {
        case true:
          i = 1;
          Console.WriteLine("When boolean is true, integer is:");
          Console.WriteLine(i);
          break;
        case false:
          i = 0;
          Console.WriteLine("When boolean is false, integer is:");
          Console.WriteLine(i);
          break;
      }
    }
  }
}

Output:

When an integer is 1, boolean is:
True
When boolean is true, integer is:
1

Use the Ternary Conditional Operator to Convert Boolean to Integer in C#

The conditional operator ?:, also known as the Ternary Conditional Operator, is similar to the if statement. It evaluates a Boolean expression and returns the result of one of two expressions.

If the Boolean expression is true, the first statement is returned (i.e., statement after the ?); the second statement is returned (i.e., statement after the :). Further discussion is available via this reference.

Below is an example of code.

// C# program to illustrate the
// use of ternary operator
using System;

class Test {
  // Main Method
  static public void Main() {
    bool boolinput = true;
    int intRresult = boolinput ? 1 : 0;
    bool boolRresult = (intRresult == 1) ? true : false;
    Console.Write("When Boolean is True, the converted integer value is: ");
    Console.WriteLine(intRresult);
    Console.Write("When integer is 1, the converted boolean value is: ");
    Console.WriteLine(boolRresult);
  }
}

Output:

When Boolean is True, the converted integer value is: 1
When integer is 1, the converted boolean value is: True

Use the if Statement to Convert From Boolean to Integer in C#

The if statement checks whether or not a specific condition is true or false after executing some logical expression. Whenever the expression returns true, the numeric value 1 is returned, 0.

Similarly, when the numeric value is 1, the Boolean value true is returned. Else, false is returned.

Below is an example of code.

// C# program to illustrate the
// use of if statement
using System;

class Test {
  // Main Method
  static public void Main() {
    bool boolinput = true;
    int intResult;
    if (boolinput) {
      intResult = 1;
    } else {
      intResult = 0;
    }

    bool boolResult;
    if (intResult == 1) {
      boolResult = true;
    } else {
      boolResult = false;
    }
    Console.Write("When Boolean is True, the converted integer value is: ");
    Console.WriteLine(intResult);
    Console.Write("When integer is 1, the converted boolean value is: ");
    Console.WriteLine(boolResult);
  }
}

Output:

When Boolean is True, the converted integer value is: 1
When integer is 1, the converted boolean value is: True
Abdullahi Salawudeen avatar Abdullahi Salawudeen avatar

Abdullahi is a full-stack developer and technical writer with over 5 years of experience designing and implementing enterprise applications. He loves taking on new challenges and believes conceptual programming theories should be implemented in reality.

LinkedIn GitHub

Related Article - Csharp Conversion

Related Article - Csharp Boolean

Related Article - Csharp Integer