Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert.ToBoolean Method in C#
The Convert.ToBoolean method in C# converts a specified value to an equivalent Boolean value. This method follows specific rules depending on the input type − numeric types return false if zero, true otherwise; string values are parsed based on their content.
Syntax
Following is the syntax for the Convert.ToBoolean method −
public static bool ToBoolean(object value); public static bool ToBoolean(string value); public static bool ToBoolean(int value); public static bool ToBoolean(double value); // Other overloads for different data types
Parameters
-
value − The value to convert to a Boolean. Can be a numeric type, string, or object.
Return Value
Returns true or false based on the conversion rules for the specific data type.
Conversion Rules
| Input Type | Returns true when | Returns false when |
|---|---|---|
| Numeric types | Value is non-zero | Value is zero |
| String | Value is "True" (case-insensitive) | Value is "False" (case-insensitive) |
| null | Never | Always returns false |
Using Convert.ToBoolean with Numeric Types
Example
using System;
public class Demo {
public static void Main() {
double doubleNum = 3.4;
int zeroInt = 0;
int nonZeroInt = 42;
bool boolFromDouble = Convert.ToBoolean(doubleNum);
bool boolFromZero = Convert.ToBoolean(zeroInt);
bool boolFromNonZero = Convert.ToBoolean(nonZeroInt);
Console.WriteLine("{0} as a Boolean = {1}", doubleNum, boolFromDouble);
Console.WriteLine("{0} as a Boolean = {1}", zeroInt, boolFromZero);
Console.WriteLine("{0} as a Boolean = {1}", nonZeroInt, boolFromNonZero);
}
}
The output of the above code is −
3.4 as a Boolean = True 0 as a Boolean = False 42 as a Boolean = True
Using Convert.ToBoolean with String Values
Example
using System;
public class StringToBoolDemo {
public static void Main() {
string trueString = "True";
string falseString = "false";
string nullString = null;
bool boolFromTrue = Convert.ToBoolean(trueString);
bool boolFromFalse = Convert.ToBoolean(falseString);
bool boolFromNull = Convert.ToBoolean(nullString);
Console.WriteLine("'{0}' as Boolean = {1}", trueString, boolFromTrue);
Console.WriteLine("'{0}' as Boolean = {1}", falseString, boolFromFalse);
Console.WriteLine("null as Boolean = {0}", boolFromNull);
}
}
The output of the above code is −
'True' as Boolean = True 'false' as Boolean = False null as Boolean = False
Exception Handling
The method throws a FormatException when the string value is not "True" or "False" −
Example
using System;
public class ExceptionDemo {
public static void Main() {
string invalidString = "maybe";
try {
bool result = Convert.ToBoolean(invalidString);
Console.WriteLine("Result: " + result);
}
catch (FormatException ex) {
Console.WriteLine("FormatException: " + ex.Message);
}
}
}
The output of the above code is −
FormatException: String 'maybe' was not recognized as a valid Boolean.
Conclusion
The Convert.ToBoolean method provides a reliable way to convert various data types to Boolean values. For numeric types, zero converts to false and non-zero to true. For strings, only "True" and "False" (case-insensitive) are valid inputs, while invalid strings throw a FormatException.
