【发布时间】:2015-04-12 03:54:17
【问题描述】:
有什么方法可以使用 C# 实现从 string 到 bool 的隐式转换?
例如我的字符串str 的值为Y,当我尝试转换(转换)为boolean 时,它必须返回true。
【问题讨论】:
-
bool b = str == "Y"; -
@NadiaChirkova,不!我的意思是:当我做 bool b = (boolean) str;
-
不,你不能那样做。
-
有一个 Boolean.Parse(myString) 和 Boolean.TryParse(myString) 但仅当字符串为“True”或“False”时才有效
-
在检查之前尝试替换您的字符串。
标签: c# .net type-conversion extension-methods implicit-conversion