调用方法里面,如果() - C# [英] Calling methods inside if() - C#
问题描述
我有一对夫妇的那取决于他们的成功返回一个布尔方法,有什么不对的调用内部的IF这些方法()?
I have a couple of methods that return a bool depending on their success, is there anything wrong with calling those methods inside of the IF() ?
//&& makes sure that Method2() will only get called if Method1() returned true, use & to call both methods
if(Method1() && Method2())
{
// do stuff if both methods returned TRUE
}
方法2()不需要火,如果方法一()返回FALSE。
Method2() doesn't need to fire if Method1() returns FALSE.
让我知道有与上面的代码中的任何问题。
Let me know there's any problem with the code above.
感谢您。
编辑:因为没有错的代码是,我会接受最翔实的答案......添加的注释来解决新手&放大器;&功放;&安培;问题
since there was nothing wrong with the code, I'll accept the most informative answer ... added the comment to solve the "newbie & &&" issue
推荐答案
我会扔在你可以使用&安培;操作者
(相对于&放大器;&放大器;
),以保证这两个方法都调用即使左手侧是假
,如果由于某种原因你希望避免短路未来。
I'll throw in that you can use the & operator
(as opposed to &&
) to guarantee that both methods are called even if the left-hand side is false
, if for some reason in the future you wish to avoid short-circuiting.
逆工程为 |运营商
,其中即使左侧条件计算为真正
,右边的条件将被评估为好。
The inverse works for the | operator
, where even if the left-hand condition evaluates to true
, the right-hand condition will be evaluated as well.
这篇关于调用方法里面,如果() - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!