C# provides a lot of inbuilt methods to do our job easier in many situations. This blog explains how to convert a boolean (bool) value into an integer (int) using Convert.ToInt32()
method.
In the below example, we use Convert.ToInt32()
method to convert the boolean value true
to 1 and a boolean value false
to 0 and use Console.WriteLine()
method to print the values in the console.
using System;
namespace ConvertBoolToInt
{
class Program
{
static void Main()
{
bool x = true;
bool y = false;
// Convert bool value to Int using ToInt32() method
Console.WriteLine(Convert.ToInt32(x));
Console.WriteLine(Convert.ToInt32(y));
Console.ReadKey();
}
}
}
Output:
1
0
Comments (0)