convert bool to int code example

Example 1: transform bool to int c#

using System; //Needed for Convert
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Convert.ToInt32(true) : " + Convert.ToInt32(true));
            Console.WriteLine("Convert.ToInt32(false): " + Convert.ToInt32(false));

            //hit ENTER to exit
            Console.ReadLine();
          //OutPut: Convert.ToInt32(true) : 1
          //		Convert.ToInt32(false): 0
          
        }
    }
}

Example 2: convert boolean to int

int val = (bool) ? 1 : 0;

Example 3: can you add a bool and an int

Yes, as a bool is 1 (true) or 0 (false)

Tags:

Java Example