How to avoid short circuit evaluation in C# while doing the same functionality

混江龙づ霸主 提交于 2019-12-18 07:35:12

问题


Do we have any operator in C# by which I can avoid short circuit evaluation and traverse to all the conditions.

say

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. ?


回答1:


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()) { }

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



回答2:


Just use a single bar;

if(txtName.Text.xyz() | txtLName.Text.xyz())
{

}


来源:https://stackoverflow.com/questions/3244316/how-to-avoid-short-circuit-evaluation-in-c-sharp-while-doing-the-same-functional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!