Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 700
Shantanu Gupta Avatar asked Jul 14 '10 08:07

Shantanu Gupta


People also ask

Does C do short-circuit evaluation?

In imperative language terms (notably C and C++), where side effects are important, short-circuit operators introduce a sequence point – they completely evaluate the first argument, including any side effects, before (optionally) processing the second argument.

What is short-circuit evaluation in decision making expressions in C#?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

What is short-circuit evaluation in the case of the and operator?

AND(&&) short circuit:If there is an expression with &&(logical AND), and the first operand itself is false, then a short circuit occurs, the further expression is not evaluated, and false is returned.


1 Answers

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
like image 121
djdd87 Avatar answered Oct 09 '22 20:10

djdd87