Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

&&= in C#? (boolean expression) [duplicate]

Possible Duplicate:
Why are there no ||= or &&= operators?

By pure accident I found out today that

 a &= GetBool();

is NOT the same as

 a = a && GetBool();

I must have misunderstood that for years. In the first Example, "GetBool()" is executed even if "a" is false. In the second, it isn't.

Is there anything to achieve something like "&&=" in C#?

like image 473
Ole Albers Avatar asked May 09 '12 09:05

Ole Albers


1 Answers

Is there anything to achieve something like "&&=" in C#?

Well, you can write

a = a && getBool();

instead... I can't think of many situations where I'd actually want to do anything different, to be honest. Even the above is pretty rare, IME.

There's no compound assignment operator defined for &&, || or ?? (which are the obvious "short-circuiting" operators). See section 7.17 of the C# 4 spec for the full list of compound assignment operators, and section 7.17.2 for more details about what exactly compound assignment does.

like image 92
Jon Skeet Avatar answered Oct 12 '22 00:10

Jon Skeet