Stu*_*ing 4 .net c# boolean-operations
我有一堆方法都返回bool.
如果一个方法返回false,则调用以下方法没有任何价值,特别是因为其中一些方法是"昂贵的"操作.
哪个更有效率?
bool result = method1();
if (result) result = method2();
if (result) result = method3();
return result;
Run Code Online (Sandbox Code Playgroud)
要么
return method1() && method2() && method3();
Run Code Online (Sandbox Code Playgroud)
据我所知,第二种形式应该在其中一种方法返回false时停止评估,对吧?