本文介绍了在执行相同功能时如何避免在C#中进行短路评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们在C#中有什么运算符可以避免短路评估并遍历所有条件.
Do we have any operator in C# by which I can avoid short circuit evaluation and traverse to all the conditions.
说
if(txtName.Text.xyz() || txtLastName.Text.xyz())
{
}
public static bool xyz(this TextBox txt)
{
//do some work.
return false;
}
它应该评估所有条件,而与获得的输出无关.并根据获得的结果评估最后的条件继续进行. ?
It should evaluate all conditions irrespective of output obtained. And after evaluating last condition continues according to result obtained. ?
推荐答案
只使用一个竖线,这将对两个参数都进行求值,而与第一个结果的结果无关.
Just use a single bar, this will evaluated both arguments regardless of the outcome of the first result.
if(txtName.Text.xyz() | txtLastName.Text.xyz()) { }
您也可以对AND进行同样的操作,即您可以将&&
替换为一个与号,以获得与上述相同的效果:
You can also do the same with AND, i.e. You can replace &&
with a single ampersand to get the same affect as above:
if(txtName.Text.xyz() & txtLastName.Text.xyz()) { } // Both sides will be called
这篇关于在执行相同功能时如何避免在C#中进行短路评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!