Boolean.TryParse() Method in C#

The Boolean.TryParse() method in C# safely converts a string representation of a logical value to its Boolean equivalent. Unlike Boolean.Parse(), this method returns true if the conversion succeeds and false if it fails, without throwing exceptions.

Syntax

Following is the syntax −

public static bool TryParse(string value, out bool result);

Parameters

  • value − A string containing the value to convert.

  • result − When this method returns, contains the Boolean value equivalent to the value contained in value, if the conversion succeeded, or false if the conversion failed.

Return Value

Returns true if the value parameter was converted successfully; otherwise, false.

Using TryParse() with Valid Boolean Strings

The method recognizes "true", "false", "True", "False" and their case variations as valid Boolean strings −

using System;

public class Demo {
   public static void Main() {
      bool result;
      bool success;
      
      success = Boolean.TryParse("true", out result);
      Console.WriteLine("Input: 'true' | Success: " + success + " | Result: " + result);
      
      success = Boolean.TryParse("False", out result);
      Console.WriteLine("Input: 'False' | Success: " + success + " | Result: " + result);
      
      success = Boolean.TryParse("TRUE", out result);
      Console.WriteLine("Input: 'TRUE' | Success: " + success + " | Result: " + result);
   }
}

The output of the above code is −

Input: 'true' | Success: True | Result: True
Input: 'False' | Success: True | Result: False
Input: 'TRUE' | Success: True | Result: True

Using TryParse() with Invalid Strings

When the input string cannot be converted to a Boolean, the method returns false and sets the result to false

using System;

public class Demo {
   public static void Main() {
      bool result;
      bool success;
      
      success = Boolean.TryParse("$", out result);
      Console.WriteLine("Input: '$' | Success: " + success + " | Result: " + result);
      
      success = Boolean.TryParse("yes", out result);
      Console.WriteLine("Input: 'yes' | Success: " + success + " | Result: " + result);
      
      success = Boolean.TryParse("1", out result);
      Console.WriteLine("Input: '1' | Success: " + success + " | Result: " + result);
      
      success = Boolean.TryParse(null, out result);
      Console.WriteLine("Input: null | Success: " + success + " | Result: " + result);
   }
}

The output of the above code is −

Input: '$' | Success: False | Result: False
Input: 'yes' | Success: False | Result: False
Input: '1' | Success: False | Result: False
Input: null | Success: False | Result: False

Comparison with Boolean.Parse()

Boolean.TryParse() Boolean.Parse()
Returns false if conversion fails Throws FormatException if conversion fails
Safe for unknown input validation Requires try-catch for error handling
Uses out parameter for result Returns Boolean directly

Conclusion

The Boolean.TryParse() method provides a safe way to convert strings to Boolean values without throwing exceptions. It returns true for successful conversions and false for failures, making it ideal for validating user input or processing uncertain data.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements