在C#中替代VB.NET的类型转换函数(CBool​​)? [英] Alternative to VB.NET's Type Conversion functions (CBool) in C#?

查看:257
本文介绍了在C#中替代VB.NET的类型转换函数(CBool​​)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB的<$ c在C#中$ c> CBool​​ 关键字?

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维克曾经在他的博客上发表过一篇很棒的文章,但是现在所有这些页面似乎都被删除了。 MSDN (在这里最准确)说:

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:


这些函数是内联编译的,这意味着转换代码是计算表达式的代码的一部分。有时,无需调用过程即可完成转换,从而提高了性能。每个函数都会将表达式强制转换为特定的数据类型。

These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression. Sometimes there is no call to a procedure to accomplish the conversion, which improves performance. Each function coerces an expression to a specific data type.

它提供的选项包括直接转换(例如: (bool)var ),尝试进行强制转换(使用 as 运算符),并调用其中定义的方法之一 System.Convert ,调用适用的 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​​)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆