是什么Convert.ToBoolean(String)和Boolean.Parse(串)之间的区别? [英] What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)?
问题描述
这是两种方法之间的差异
What is the difference between the two methods
Convert.ToBoolean()
和
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?
谢谢,
太
推荐答案
Convert.ToBoolean(串)
实际上调用 bool.Parse()
无论如何,所以对于非空字符串
S,没有功能上的差异。 (对于空字符串
S, Convert.ToBoolean()
收益假
,而 bool.Parse()
引发 ArgumentNullException
)
Convert.ToBoolean(string)
actually calls bool.Parse()
anyway, so for non-null string
s, there's no functional difference. (For null string
s, Convert.ToBoolean()
returns false
, whereas bool.Parse()
throws an ArgumentNullException
.)
鉴于这一事实,你应该使用 bool.Parse()
当你确定你的输入不为空,因为你救自己一空检查。
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()
当然有许多重载,让您生成一个布尔
从许多其他内置类型,而解析()
是字符串
唯一的。
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 string
s only.
在type.Parse方面()方法,你应该看出来,所有的内置数值类型有解析()
以及的TryParse()
方法。 的DateTime
有这些,还有其他 ParseExact()
/ TryParseExact()
方法,它允许你指定的日期预期格式。
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.
这篇关于是什么Convert.ToBoolean(String)和Boolean.Parse(串)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!