John Livermore
John Livermore

Reputation: 31313

&= operator in C# - does it work with boolean types?

I have an easy question that I think I know the answer but I will ask here.

Is the code...

var test = true;
test = test && true;  // test remains true
test = test && false;  // test is now false

Equivalent to the code...

var test = true;
test &= true;  
test &= false; 

&= is a bitwise operation, so I am surprised the compiler will even allow this?

Upvotes: 2

Views: 86

Answers (1)

Red Star
Red Star

Reputation: 667

It works, but not the same way as in first code. The equivalent code:

var test = true;
test = test & true; 
test = test & false;

C# doesn't have &&= operator

Upvotes: 2

Related Questions