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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.