Tag: boolean operations

用&&​​链接方法

我有一堆方法都返回bool。 如果一个方法返回false,则调用以下方法没有任何价值,特别是因为其中一些方法是“昂贵的”操作。 哪个更有效率? bool result = method1(); if (result) result = method2(); if (result) result = method3(); return result; 要么 return method1() && method2() && method3(); 据我了解,第二种forms应该在其中一种方法返回false时停止评估,对吧?

为什么C#中的1 && 2是假的?

我对另一个问题感到沮丧 。 所以我写了这个例子。 在C中,以下是真实的。 见演示 int main() { printf(“%d”, 1 && 2); return 0; } 输出: 1 在C#中。 这是假的。 为什么这是假的? 此外,我不明白为什么我需要在这个例子中创建bool运算符,但不是在我的另一个问题,但无论如何。 为什么以下是假的? 对我来说完全是无稽之谈。 这里描述了使得下面的假的逻辑 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyInt a=1, b=2; bool res=a && b; Console.WriteLine(“result is {0}”, res); } […]