gpt4 book ai didi

.net - 替代 C# 中 VB.NET 的类型转换函数 (CBool​​)?

转载 作者:行者123 更新时间:2023-12-04 22:07:38 25 4
gpt4 key购买 nike

在 C# 中是否有替代 VB 的 CBool 关键字的方法?

所有其他功能呢?
CBool 将转换为任何有效 bool 值的 bool 值: 0"False"null 等。

最佳答案

诀窍是VB.NET 中的Cxx“函数”实际上并不是函数。事实上,它们更像是编译器将其转换为它确定的“最佳匹配”类型转换的运算符。

Paul Vick 曾经在他的博客上有一篇关于此的很棒的文章,但现在所有这些页面似乎都已被删除。 MSDN(这里大部分是准确的)说:

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 class 中定义的方法之一、调用适用的 Type.Parse 方法,以及其他一些策略。

在 C# 中没有直接的等价物:你必须按照编译器的想法来代替。

在这种情况下,您几乎肯定希望使用 Convert.ToBoolean 方法的适当重载,因为该特定方法将具有将值转换为 bool 的必要逻辑。直接 Actor 在这里不起作用。

关于.net - 替代 C# 中 VB.NET 的类型转换函数 (CBool​​)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6520809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com