Can .NET convert quot;Yesquot; amp; quot;Noquot; to boolean without If?(.NET 可以转换“是吗?amp;“不没有 If 的布尔值?)
问题描述
你会认为会有一种使用 DirectCast、TryCast、CType 等的方法,但它们似乎都被它窒息了,例如:
You would think there would be a way using DirectCast, TryCast, CType etc but all of them seem to choke on it e.g.:
CType("Yes", Boolean)
你得到:
System.InvalidCastException -从字符串是"转换为类型布尔值"无效.
System.InvalidCastException - Conversion from string "Yes" to type 'Boolean' is not valid.
推荐答案
如果你仔细想想,yes"不能转换为 bool,因为它是一个语言和上下文特定的字符串.
If you think about it, "yes" cannot be converted to bool because it is a language and context specific string.
是"不是真实的同义词(尤其是当你的妻子这么说时......!).对于这样的事情,您需要自己进行转换;yes"表示真",mmmm yeeessss"表示半真半假,也许",等等.
"Yes" is not synonymous with true (especially when your wife says it...!). For things like that you need to convert it yourself; "yes" means "true", "mmmm yeeessss" means "half true, half false, maybe", etc.
这篇关于.NET 可以转换“是"吗?&“不"没有 If 的布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET 可以转换“是"吗?&“不"没有 If 的布尔值?


基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01