【问题标题】:Is there a bool.Implies(bool) extension method in C#/.NET?C#/.NET 中是否有 bool.Implies(bool) 扩展方法?
【发布时间】:2013-11-21 15:55:47
【问题描述】:

bool.Implies(bool) 定义在哪个库中?

我想像Assert.That (a.Implies(b))一样使用它。

我有一个实现,但宁愿使用内置的。

bool Implies(this bool a, bool b){
    return !a || b;
}

【问题讨论】:

标签: c# .net extension-methods boolean-logic


【解决方案1】:

VB 6 中有一个Imp 运算符的实现。您可以在Microsoft.VisualBasic.Compatibility.VB6 命名空间下的Support.Imp 中找到它。但是,对于这么简单的事情,我建议您只使用您自己的方法,而不是仅仅为此方法引入额外的依赖项。

【讨论】:

  • +1 表示神秘的发现,文档中的 Note: This API is now obsolete. 字样不会激发信心。很好的推荐。
【解决方案2】:

我认为图书馆里没有任何东西。所以我自己写了。

static class myLogicExtensions {
    public static bool Implies(this bool a, bool b){
        return !a || b;
    }
}

【讨论】:

  • 这看起来很奇怪 - 所以false.Implies(true) = true?
  • @D 斯坦利。是的,这听起来很奇怪。但是请记住,左边的假和真是关于不同的事情的。因此,让我们输入一些真实的谓词:例如,如果您乘坐自动扶梯,则必须穿鞋escalator.amRiding implies shoes.amWearing。如果这个谓词返回false,那么安全警察就会进来。其他例子is computer implies has memoryis tree implies has branchesis circle implies is not squarelight is on implies switch is on,开关打开并不意味着灯亮,而是switch is off implies light is off
  • 我想除了最后一部分之外我都遵循了这一点:light is on implies switch is onlight is off !implies switch is off?因为灯泡可能会烧坏?但是如果开关关闭它肯定不能打开,对吧?
猜你喜欢
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多