c# - 如果变量不是 boolean 值则返回 False

标签 c# boolean

我怎样才能让下面的代码返回 False如果usableTrue以外的任何东西(除 bool 以外的任何内容),目前我的代码抛出异常 usable不是 boolean 值。

if (!Boolean.Parse(readValue("Useable"))) return true;
return (defined.ContainsKey(key) || (key == "Useable"));

最佳答案

bool isUseable;
bool.TryParse(readValue("Useable"), out isUseable);

关于c# - 如果变量不是 boolean 值则返回 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1465937/

相关文章:

c# - Windows 8 : How to undo & redo ink using built in Inking functionality?

c - 这里的返回是如何进行的?

function - 传递给函数的 boolean 值未按预期工作

ios - 从 Objective-C 中的 ColdFusion boolean 返回类型获取 BOOL 的更好方法?

c# - 如何使用包含其他模型列表的模型创建下拉列表?

C# - 从列表框中删除所有项目

c# - 缓慢嵌套 'for' 循环读取 Excel 对象

c# - 仅检查数组或列表中的特定 boolean 值

c++ - 为什么 shared_ptr 在函数中返回时没有隐式转换为 boolean 值?

c# - 如何设置图像评级?