Home » C#.Net

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

C# Convert.ToInt32(bool) Method: Here, we are going to learn how to convert a bool value to an integer value in C#?
Submitted by IncludeHelp, on February 10, 2019

C# Convert.ToInt32(bool) Method

Convert.ToInt32(bool) Method is used to convert a specific Boolean (bool) value to its equivalent integer (int 32 signed number).

Syntax:

    int Convert.ToInt32(bool value);

It accepts a bool value/variable as an argument and returns its equivalent signed 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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.