C# Convert.ToInt32(bool) Method – Convert bool value to int

C# Convert.ToInt32(bool) Method: Here, we will figure out how to change over a bool value to an integer value in C#?

C# Convert.ToInt32(bool) Method

Convert.ToInt32(bool) Method is utilized to change over a particular Boolean (bool) value to its proportional integer (int 32 marked number).

Syntax:

    int Convert.ToInt32(bool value);

It acknowledges a bool value/variable as contention and returns its proportional marked integer.

Example:

    Input: 
    bool a = true;
    Output:
    1

Code:

using System;
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));

            bool a = true;
            bool b = false;

            Console.WriteLine("Convert.ToInt32(a) : " + Convert.ToInt32(a));
            Console.WriteLine("Convert.ToInt32(b): " + Convert.ToInt32(b));

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Convert.ToInt32(true) : 1
Convert.ToInt32(false): 0
Convert.ToInt32(a) : 1
Convert.ToInt32(b): 0

Leave a Comment

error: Alert: Content is protected!!