94

What is the difference between the two methods

Convert.ToBoolean()

and

Boolean.Parse()?

Is there any reason to use one or the other?

Additionally, are there any other type.Parse() methods that I should watch out for?

Thanks,

Matt

3 Answers 3

92

Convert.ToBoolean(string) actually calls bool.Parse() anyway, so for non-null strings, there's no functional difference. (For null strings, Convert.ToBoolean() returns false, whereas bool.Parse() throws an ArgumentNullException.)

Given that fact, you should use bool.Parse() when you're certain that your input isn't null, since you save yourself one null check.

Convert.ToBoolean() of course has a number of other overloads that allow you to generate a bool from many other built-in types, whereas Parse() is for strings only.

In terms of type.Parse() methods you should look out for, all the built-in numeric types have Parse() as well as TryParse() methods. DateTime has those, as well as the additional ParseExact()/TryParseExact() methods, which allow you specify an expected format for the date.

2
  • Thanks for the details. So, there is no difference in performance between the two, or you could say extremely small gains by using Boolean.Parse(string)?
    – brandeded
    Aug 11, 2011 at 20:04
  • 1
    @mbrownnyc You would get a very minor (likely imperceptible) gain by using Parse() if you're certain the input isn't a null string. If you're not sure, just use ToBoolean(), since it will do the check for you. And if you don't want a null string to result in false, then perform the check yourself, and then call Parse() if it's not null.
    – dlev
    Aug 11, 2011 at 20:05
43

Here is the short demo:

object ex1 = "True";
Console.WriteLine(Convert.ToBoolean(ex1)); // True
Console.WriteLine(bool.Parse(ex1.ToString())); // True

object ex2 = "true";
Console.WriteLine(Convert.ToBoolean(ex2)); // True
Console.WriteLine(bool.Parse(ex2.ToString())); // True

object ex3 = 1;
Console.WriteLine(Convert.ToBoolean(ex3)); // True
Console.WriteLine(bool.Parse(ex3.ToString())); // Unhandled Exception: System.FormatException

object ex3 = "1";
Console.WriteLine(Convert.ToBoolean(ex3)); // An unhandled exception of type 'System.FormatException' occurred
Console.WriteLine(bool.Parse(ex3.ToString())); // Unhandled Exception: System.FormatException

object ex4 = "False";
Console.WriteLine(Convert.ToBoolean(ex4)); // False
Console.WriteLine(bool.Parse(ex4.ToString())); // False

object ex5 = "false";
Console.WriteLine(Convert.ToBoolean(ex5)); // False
Console.WriteLine(bool.Parse(ex5.ToString())); // False

object ex6 = 0;
Console.WriteLine(Convert.ToBoolean(ex6)); // False
Console.WriteLine(bool.Parse(ex6.ToString())); // Unhandled Exception: System.FormatException

object ex7 = null;
Console.WriteLine(Convert.ToBoolean(ex7)); // False
Console.WriteLine(bool.Parse(ex7.ToString())); // Unhandled Exception: System.NullReferenceException

Note: There are also two properties of bool TrueString and FalseString, but be careful, because bool.TrueString != "true", only bool.TrueString == "True"

Console.WriteLine(bool.TrueString); // True
Console.WriteLine(bool.FalseString); // False
2
  • 2
    Convert.ToBoolean("1"); // Format Exception May 16, 2016 at 15:30
  • Convert.ToBoolean("2"); will also return a truthy value. I know that booleans are sometimes referenced as 0 and 1, but you never know that someone mistypes something.
    – Arne Peirs
    Jul 10, 2021 at 13:42
3

Boolean.Parse() will convert a string representation of a logical boolean value to a boolean value. Convert.ToBoolean() has multiple overloads that will convert primitive types to their boolean equivalent.

Most, if not all, of the primitive types in C# have an associated *.Parse/Convert.To* method that serve the same purpose as Boolean.Parse()/Convert.ToBoolean().

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.