What is the difference between Convert.ToBoolean (string) and Boolean.Parse (string)?

What is the difference between the two methods

Convert.ToBoolean()

and

Boolean.Parse() ?

Is there any reason to use one or the other?

Also, are there any other type.Parse() methods that I must comply with?

Thank,

Matt

+55
c # parsing boolean
Aug 11 '11 at 19:52
source share
3 answers

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

Given this fact, you should use bool.Parse() when you are sure that your input is not null, since you keep one null check.

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

Regarding type methods. Parsse (), which you should consider, all built-in numeric types have Parse() , as well as TryParse() methods. DateTime has those, as well as optional ParseExact() / TryParseExact() methods that let you specify the expected date format.

+56
Aug 11 2018-11-11T00:
source share

Here is a 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 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 
+17
Oct 05 '14 at 12:29
source share

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

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

+2
Aug 11 '11 at 19:54
source share



All Articles