
Ints and bools are both value types. How can you convert an int in your C# program to a bool value? This can be done with the Convert.ToBoolean method in the System namespace. We show an example of converting an integer into a boolean.
This C# example program converts ints to bools. The result is true and false values.
First, this program uses the Convert type to convert ints into bools. The Convert.ToBoolean method is perfect for this purpose. This method considers 0 to be false. All other values, positive and negative, are true.
Program that converts int to bool [C#]
using System;
class Program
{
static void Main()
{
Console.WriteLine(Convert.ToBoolean(5));
Console.WriteLine(Convert.ToBoolean(0));
Console.WriteLine(Convert.ToBoolean(-1));
}
}
Output
True
False
True
You can convert bools and ints in the C# programming language in a variety of ways. With the Convert.ToBoolean method, zero is considered false; all other values are true. Check out the article about converting bools into ints as well.
Convert Bool to Int Cast Examples