.NET Backend CodeTip

CodeTip #5 – How to avoid short-circuiting evaluation in C#?

Yet another week, yet another code tip for you folks! Today’s post is going to be short and easy but I hope that useful at the same time (at least for the C# beginners). Let’s start!

 

C# logical operators

One of the first things we learn when starting discovering some new programming language are operators. There are plenty of them including arithmetic operators, bitwise operators and… logical operators.  In C# two most common ones are „&&” for conjunction and „||” for an alternative. The usage is very simple:

 

        if(a && && b && c)
        {            
        }

        if(a || b || c)
        {           
        }

 

Now, let’s move back to some math basics. The whole conjunction is false if at least of its variables is false. On the other hand, the alternative is true if at least one of its variables is true. Simple is that. The question here is: Does C# CLR know that? To find out let’s write some code:

 


class Program
{
    static void Main(string[] args)
    {
        if(FirstLogicalAction() || SecondLogicalAction())
            Console.WriteLine("The alternative is true");

        Console.ReadLine();
    }

    static bool FirstLogicalAction()
    {
        Console.WriteLine("Executing first logical action");
        return true;
    }

    static bool SecondLogicalAction()
    {
        Console.WriteLine("Executing second logical action")
        return false;
    }
}


 

What we’ve got here are two methods that imitate some actions which should result with boolean. What result should satisfy us? Since we deal with alternative inside the if statement and first method return true, the result should be printing only two texts (without „Executing second logical action”). I hope that’s clear – simply no need to check whether all variables are true since the first one makes the whole expression true. Let’s run the program and find out how does it work:

 

 

That’s a good one! The result is correct because two above operators perform a short-circuiting evaluation. Using simpler words, as soon as the result of the entire expression is known the calculation is terminated (right operand is not checked). That’s an efficient and handy in most cases. But sometimes it would be nice to check every single variable (or method like in the example we’ve done so far). How can we achieve that?

 

Avoiding short-circuiting evaluation

In addition to „&&” and „||” operators, C# delivers also two, less known – „&” and „|„. These don’t perform a short-circuiting evaluation. Let’s modify our program:

 


class Program
{
    static void Main(string[] args)
    {
        if(FirstLogicalAction() | SecondLogicalAction())
            Console.WriteLine("The alternative is true");

        Console.ReadLine();
    }

    static bool FirstLogicalAction()
    {
        Console.WriteLine("Executing first logical action");
        return true;
    }

    static bool SecondLogicalAction()
    {
        Console.WriteLine("Executing second logical action")
        return false;
    }
}


 

After executing, we’ve got:

 

Remember that using the „&” and  „|” everywhere might cause serious performance issues. That’s why in most cases the proper operators are those which perform a short-circuiting evaluation.

 

Don’t miss new posts!

If you enjoy reading my blog, follow me on Twitter or leave a like on Facebook. It costs nothing and will let you be up to date with new posts 🙂