TechieClues TechieClues
Updated date Jan 31, 2023
This blog will show you how to convert a bool to an int in C#

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

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!