问题描述
VB的关键字?
Is there any alternative to VB's CBool
keyword in C#?
其他所有功能呢?
Cool
会将布尔值转换为布尔值: 0
, False
, null
等。
CBool
will turn to a Boolean any valid boolean: 0
, "False"
, null
etc.
推荐答案
诀窍在于,VB.NET中的 Cxx
功能实际上并没有发挥作用。实际上,它们更像是 operators ,编译器会将其转换为确定为最佳匹配类型的转换。
The trick is that the Cxx
"functions" in VB.NET aren't actually functions. In fact, they're more like operators that the compiler translates to what it determines is the "best-match" type conversion.
Paul维克曾经在他的博客上发表过一篇很棒的文章,但是现在所有这些页面似乎都被删除了。 (在这里最准确)说:
Paul Vick used to have a great article about this on his blog, but all those pages seem to have been taken down now. MSDN (which is mostly accurate here) says:
它提供的选项包括直接转换(例如: (bool)var
),尝试进行强制转换(使用 as
运算符),并调用其中定义的方法之一,调用适用的 Type.Parse
方法以及其他一些策略。
The options it has available to do so include a direct cast (such as: (bool)var
), an attempt to cast (using the as
operator), calling one of the methods defined in the System.Convert
class, calling the applicable Type.Parse
method, and maybe some other strategies.
没有直接等效于C#:您必须做编译器的思考。
There's no direct equivalent of this in C#: you have to do the compiler's thinking instead.
在这种情况下,您几乎肯定要使用 Convert.ToBoolean
方法因为该特定方法将具有将值转换为 bool
的必要逻辑。直接投射在这里不起作用。
In this case, you'll almost certainly want to use the appropriate overload of the Convert.ToBoolean
method because that particular method will have the necessary logic to convert the value into a bool
. A direct cast won't work here.
这篇关于在C#中替代VB.NET的类型转换函数(CBool)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!