将在一个逻辑表达式的所有方法被执行? [英] Will all methods in a logical expressions be executed?
本文介绍了将在一个逻辑表达式的所有方法被执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,给出的两种方法
In C#, given the two methods
bool Action1(object Data);
bool Action2(object Data);
这是在如果
语句一样使用这样的:
that are used in an if
statement like this:
if ( Action1(Data) || (Action2(Data) )
{
PerformOtherAction();
}
将措施2()
仍然被称为如果措施1()
返回真,或者是这个由编译器优化阻止,因为它是已知的表达式会真正
?
will Action2()
still be called if Action1()
returned true, or is this prevented by compiler optimisation, since it is already known that the expression will evaluate to true
?
推荐答案
措施2()将仅在措施1()返回false名为
Action2() will only be called if Action1() returns false
这是概念上类似于
if (Action1(Data))
{
PerformOtherAction();
}
else if (Action2(Data))
{
PerformOtherAction();
}
这篇关于将在一个逻辑表达式的所有方法被执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文