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
Boolean.Parse() Method in C#
The Boolean.Parse() method in C# is used to convert the specified string representation of a logical value to its Boolean equivalent. This method converts strings like "true", "false", "TRUE", or "FALSE" to their corresponding boolean values.
Syntax
Following is the syntax −
public static bool Parse(string value);
Parameters
value − A string containing the value to convert. It can be
"true","false","True","False","TRUE", or"FALSE".
Return Value
Returns true if the value is equivalent to TrueString, or false if the value is equivalent to FalseString.
Using Boolean.Parse() with String Literals
Example
using System;
public class Demo {
public static void Main() {
bool b1 = bool.Parse("FALSE");
bool b2 = bool.Parse("true");
bool b3 = bool.Parse("True");
bool b4 = bool.Parse("FALSE");
Console.WriteLine("Parsing 'FALSE': " + b1);
Console.WriteLine("Parsing 'true': " + b2);
Console.WriteLine("Parsing 'True': " + b3);
Console.WriteLine("Parsing 'FALSE': " + b4);
}
}
The output of the above code is −
Parsing 'FALSE': False Parsing 'true': True Parsing 'True': True Parsing 'FALSE': False
Using Boolean.Parse() with TrueString and FalseString
Example
using System;
public class Demo {
public static void Main() {
bool b1 = bool.Parse(bool.TrueString);
bool b2 = bool.Parse(bool.FalseString);
Console.WriteLine("TrueString value: " + bool.TrueString);
Console.WriteLine("FalseString value: " + bool.FalseString);
Console.WriteLine("Parsing TrueString: " + b1);
Console.WriteLine("Parsing FalseString: " + b2);
}
}
The output of the above code is −
TrueString value: True FalseString value: False Parsing TrueString: True Parsing FalseString: False
Handling Parse Exceptions
The Boolean.Parse() method throws a FormatException if the input string is not a valid boolean representation −
Example
using System;
public class Demo {
public static void Main() {
string[] values = {"true", "false", "invalid", "1", "0"};
foreach (string value in values) {
try {
bool result = bool.Parse(value);
Console.WriteLine("'{0}' parsed successfully: {1}", value, result);
}
catch (FormatException) {
Console.WriteLine("'{0}' - Invalid format for Boolean.Parse()", value);
}
}
}
}
The output of the above code is −
'true' parsed successfully: True 'false' parsed successfully: False 'invalid' - Invalid format for Boolean.Parse() '1' - Invalid format for Boolean.Parse() '0' - Invalid format for Boolean.Parse()
Boolean.Parse() vs Boolean.TryParse()
| Boolean.Parse() | Boolean.TryParse() |
|---|---|
| Throws FormatException for invalid input | Returns false for invalid input without throwing exception |
| Directly returns boolean value | Returns success status and outputs boolean value via out parameter |
| Best when input validity is guaranteed | Best when input validity is uncertain |
Conclusion
The Boolean.Parse() method converts valid string representations like "true" or "false" to boolean values. It throws a FormatException for invalid inputs, so use Boolean.TryParse() when you need safer parsing without exceptions.
