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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Convert.ToBoolean(string)actually callsbool.Parse()anyway, so for non-nullstrings, there’s no functional difference. (For nullstrings,Convert.ToBoolean()returnsfalse, whereasbool.Parse()throws anArgumentNullException.)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 aboolfrom many other built-in types, whereasParse()is forstrings only.In terms of type.Parse() methods you should look out for, all the built-in numeric types have
Parse()as well asTryParse()methods.DateTimehas those, as well as the additionalParseExact()/TryParseExact()methods, which allow you specify an expected format for the date.