0

I am setting the visibility of a button and I have code like

    btnReset.Visible |= newCondition();

What I noticed is |= is not short circuited, and ||= does not exist. Granted I can rewrite the statement as

    btnReset.Visible = btnReset.Visible || newCondition();

But I was just curious if there was a short hand short circuit operator?

Below is test code to verify that |= is not short circuit

    static void Main(string[] args)
    {

        bool firstValue = true;

        // Short Circuit 
        bool secondValue = firstValue || CallThisFunction();

        // Full Evaluation 
        bool thirdValue = firstValue | CallThisFunction();

        // Full Evaluation 
        secondValue |= CallThisFunction();
    }

    private static bool CallThisFunction()
    {
        return true;
    }
Mike
  • 5,918
  • 9
  • 57
  • 94
  • 3
    Well you can't short-circuit |= because it's a bitwise operator. Just because the LHS is non-zero doesn't mean the value of the RHS wouldn't have an effect on the result. – Matthew Watson Mar 24 '16 at 13:02
  • Sure but is there a equivalent to ||= – Mike Mar 24 '16 at 13:03
  • 2
    Nope, you have to use the longhand. – Matthew Watson Mar 24 '16 at 13:04
  • To extend upon to @MatthewWatson , the bitwise operator works with booleans properly because of binary representation of true and false. In VB.NET these are also often confused, `And` isnt the same as `AndAlso` , its the same here. – Mafii Mar 24 '16 at 13:04
  • I don't think it is a dupe because the question you reference is "Why" doesn't it... I am asking "Is there another way" – Mike Mar 24 '16 at 13:05
  • [Difference betwen `|` and `||`](http://stackoverflow.com/q/35301/1997232). – Sinatr Mar 24 '16 at 14:12
  • Again I do know the difference, as you see from the code example, the question was not "What" or "Why" but "Is this function available" – Mike Mar 24 '16 at 14:32

0 Answers0