Convert string to bool in C#

To convert a string to a bool in C#, you can use several methods. The most common approaches are bool.Parse(), bool.TryParse(), and Convert.ToBoolean(). Each method has different behaviors for handling invalid input.

Syntax

Following is the syntax for converting string to bool using different methods −

bool result = bool.Parse(stringValue);
bool result = Convert.ToBoolean(stringValue);
bool.TryParse(stringValue, out bool result);

Using bool.Parse()

The bool.Parse() method converts a string representation of a boolean value to its boolean equivalent. It accepts "True" or "False" (case-insensitive) and throws an exception for invalid values −

using System;

class Demo {
   static void Main() {
      string str1 = "false";
      string str2 = "True";
      string str3 = "FALSE";
      
      bool res1 = bool.Parse(str1);
      bool res2 = bool.Parse(str2);
      bool res3 = bool.Parse(str3);
      
      Console.WriteLine("'" + str1 + "' converts to: " + res1);
      Console.WriteLine("'" + str2 + "' converts to: " + res2);
      Console.WriteLine("'" + str3 + "' converts to: " + res3);
   }
}

The output of the above code is −

'false' converts to: False
'True' converts to: True
'FALSE' converts to: False

Using bool.TryParse()

The bool.TryParse() method is safer as it returns false if the conversion fails instead of throwing an exception −

using System;

class Demo {
   static void Main() {
      string[] testValues = {"true", "false", "invalid", "1", ""};
      
      foreach(string value in testValues) {
         bool result;
         bool success = bool.TryParse(value, out result);
         
         Console.WriteLine("Value: '{0}' | Success: {1} | Result: {2}", 
                          value, success, result);
      }
   }
}

The output of the above code is −

Value: 'true' | Success: True | Result: True
Value: 'false' | Success: True | Result: False
Value: 'invalid' | Success: False | Result: False
Value: '1' | Success: False | Result: False
Value: '' | Success: False | Result: False

Using Convert.ToBoolean()

The Convert.ToBoolean() method can handle more input types and interprets numeric values differently from bool.Parse()

using System;

class Demo {
   static void Main() {
      string[] values = {"true", "false", "1", "0"};
      
      foreach(string value in values) {
         try {
            bool result = Convert.ToBoolean(value);
            Console.WriteLine("'{0}' converts to: {1}", value, result);
         }
         catch(FormatException) {
            Console.WriteLine("'{0}' - Invalid format", value);
         }
      }
   }
}

The output of the above code is −

'true' converts to: True
'false' converts to: False
'1' - Invalid format
'0' - Invalid format

Comparison of Methods

Method Valid Input Invalid Input Behavior
bool.Parse() "True", "False" (case-insensitive) Throws FormatException
bool.TryParse() "True", "False" (case-insensitive) Returns false, sets result to false
Convert.ToBoolean() "True", "False" (case-insensitive) Throws FormatException

Conclusion

Use bool.Parse() when you're certain the input is valid, bool.TryParse() for safe conversion without exceptions, and Convert.ToBoolean() when working with various data types. The TryParse() method is generally recommended for user input validation.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements