c sharp

How to Convert a Boolean to an Int32 in C#

In C# several datatypes are used to store and declare data and variables, methods, and classes. Data cannot be stored without declaring its type. In C#, boolean datatypes only have the options of “true” and “false”. Most frequently, it is applied when making decisions, or comparison is necessary. With the aid of several classes and methods, one type can become another in C#.

In this article, the conversion of boolean to Int32 type is discussed.

How to Convert a Boolean Value to an Int32 in C#

An “Int32” type represents an integer of 32 signed bits. Its minimum range is “-2,147,483,648” whereas its maximum range is “2,147,483,648”. On the other hand “boolean” is used in situations that involve comparisons. It has only two values “true” and “false” which when converted to int type represent “0” and “1”.

  • 0” represents “false
  • 1” represents “true

Method
To convert a boolean to “Int32” the “Convert.ToInt32()” method is used. In this method “Convert” is the class name that is present in C# that is used for the conversion of one datatype to other datatypes and “ToInt32” represents the type “Int32” in which we will convert the data.

Syntax
The syntax of the “Convert.ToInt32()” method is:

Convert.ToInt32(value);

Parameters
In the above syntax “Convert.ToInt32()” is the method name and “value” represents the input boolean value that will be converted.

Return Type
This method returns an integer with the values “0” or “1” as its return type.

Example of Conversion of Boolean to Int32 in C#
The following code example will show a simple program to convert a boolean to Int32.

using System;

public class Bool_to_int32_example
{
    public static void Main(string[] args)
    {
            bool a = true;
            bool b = false;
            Console.WriteLine("The converted value of true to Int 32 is: "+Convert.ToInt32(a));
            Console.WriteLine("The converted value of false to Int 32 is: "+Convert.ToInt32(b));
    }
}

In the above-stated code:

  • First of all, the “System” namespace is used.
  • Then there is a class “Bool_to_int32_example” that has a “Main()” method.
  • After that two boolean variables “a” and “b” are declared.
  • The value of “a” is “true” whereas the value of “b” is “false”.
  • Lastly, the “Convert.ToInt32()” method is called for both variables that will convert the value of “true” and “false” to “Int32”.

Output

Here the output shows that:

  • The value of “true” in “Int32” is “1”.
  • Contrarily, “Int32″‘s “false” value is “0”.

Conclusion

In C# various datatypes are used to store data. These datatypes define which type of data that variable, method, or class contains. Data stored in one datatype can be converted to others using different classes and functions. Use the “Convert.ToInt32()” function to change a boolean value to an Int32.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.