0

Consider the following code:

bool funcExecuted = false;
bool result = false;
Func<bool> returnFalse = () =>
{
    funcExecuted = true;
    return false; 
};
result &= returnFalse();

string msg = funcExecuted?"Executed":"Not Executed";
Console.WriteLine(msg);

Expected result:

  • I expected to get the message of Not Executed

Actual result:

  • I got the Executed message.

Conclusions:

result &= returnFalse(); is compiled to result = result & returnFalse(); rather than result = result && returnFalse(); (I expected the latter).

My question is, why is that?

I would expect it to be the other way around. We all use the && operator much more commonly than the & operator and I would expect it to be the "default behaviour".


Update: Most answers seems to answer the wrong question.
I am aware of the fact that the difference between && and & is that && is short-circuiting, whereas & always evaluates both operands.

The question was why does a &= b translates into a = a & b; rather than a = a && b;

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
  • Presumably it would be called &&= in that case. – Mark Pattison Jul 01 '14 at 09:44
  • possible duplicate of [What does the "&=" in this C# code do?](http://stackoverflow.com/questions/4556027/what-does-the-in-this-c-sharp-code-do) – shree.pat18 Jul 01 '14 at 09:44
  • @MarkPattison Thought about it too, was googling to check if this operator exists, and I am just unaware of it. – Avi Turner Jul 01 '14 at 09:45
  • 1
    well its a perception you have. As we know x+=y means = x+y and I am personally use to this perception. Thus for x &= y I will expect x = x & y – Savaratkar Jul 01 '14 at 09:46
  • 2
    Why would you expect a compound assignment operator which only uses one `&` to be equivalent to using `&&` rather than `&`? That seems an odd expectation to me. – Jon Skeet Jul 01 '14 at 09:46
  • `&=` is a bitwise operator in C, C++, Java and even JavaScript. As a member of the C-like "dysfunctional family" of languages, C# sticks with the established pattern when it comes to fundamentals like this. – Daniel Earwicker Jul 01 '14 at 09:50
  • @jonskeet I figured that the more commonly used operator should deserve a more convenient way for typing, and since I am not aware of C# having 3 characters operators (e.g. `&&=` as suggested) this would be the appropriate one. perhaps it is only me... – Avi Turner Jul 01 '14 at 09:50
  • See http://stackoverflow.com/questions/6346001/why-are-there-no-or-operators – Rotem Jul 01 '14 at 10:10
  • @AviTurner: I can understand thinking there should be an `&&=` operator... but that's not the same as expecting the `&=` operator to use `&&`. – Jon Skeet Jul 01 '14 at 10:26

3 Answers3

2

The confusion that you fail into is that when there is a difference when you use && and & on numeric values but there is not when you use it on booleans in context of bytes.

Then only difference is that when you use &&, you get as short-circuiting.

boolean result = false  & true; //bitwise AND
boolean result = false && true; //logical AND

There is no difference in result. Then only difference is how CPU will deal with it.

In that case when you are using operator &= for boolean it is equal to use result = value & value.

The assumption that short-circuiting, will be used when you use &= is not easy to understand. The argument that && is used more often than & does not find place here. Because the & is bitwise logical operator and && is logical. So it is natural that &= mean what it mean.

1

You got "Executed" message because of

Func<bool> returnFalse = () =>
{
    funcExecuted = true; //THIS !!!!!!
    return false; 
};

and this

string msg = funcExecuted?"Executed":"Not Executed";

funcExceuted is ALWAYS (in the scope of provided code) true.

The code of binary & you wrote is actually converted to what you expect:

result &= returnFalse(); 

is

result = result &  returnFalse();

but the thing is that on condition you don't check for result, but for funcExceuted. change it ti result and you will get expected behavior from code.

What about

why is that?

it's C# language syntax, for the same reson

left+=right is converted to left = left + right

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 2
    He's not asking why the result is what it is, he wants to know if there's a shorthand for `var a = a && b` or why `&=` results in `a & b` and not `a && b`. – germi Jul 01 '14 at 09:50
  • @germi: he is asking why the code is not printing "Not executed", answer is: the codintion checks wrong variable. – Tigran Jul 01 '14 at 09:52
  • 2
    @Tigran He expected the function not to execute at all, as `result && returnFalse()` would short cicruit because `result` starts out `false`. – Rotem Jul 01 '14 at 09:54
  • @Rotem: but he wrote & and NOT &&. He knows what is & and what is && and knows differences between them, but whoud expect x&=y equal to x = x&&y, so my answer adresses that issue *too*. – Tigran Jul 01 '14 at 09:56
  • @Tigran I guess the only real question here is 'Is there an `&&=` operator in c#'. – Rotem Jul 01 '14 at 10:05
0

One of the key differences between && and & is that && is short-circuiting, whereas & always evaluates both operands.

The result &= returnFalse(); is eqivalent to result = result & returnFalse();.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • Thanks for your answer. I am aware of that. As I wrote, I have expected it to be equivalent to `result = result && returnFalse();` – Avi Turner Jul 01 '14 at 09:47
  • If you knew it, why ask the question? Or were you hoping someone here would communicate with the gods of C#? – david.pfx Jul 01 '14 at 09:49
  • @david.pfx Sorry for the typo. I meant `result = result && returnFalse();` - comment updated. The question is about is why is that. – Avi Turner Jul 01 '14 at 09:52