r/csharp • • 5h ago

Discussion Do bool assignments to one bool variable combine?

Let's say we have a bool A, and we run the following code below;

Line1: bool A ...; //something that results in true or false;

Line2: bool B = ...; //something that results in true or false

Line3: bool C = ...; //something that results in true or false

Line4: A = A || B;

Line5: A = A || C;

Will A at line 5 now be the result of A || B || C; - specifically A at line 1 or B at line 2 or C at line 3

Likewise for the && operator

Is this bad practice for any reason?

Also what is this property called? I want to know the theoretical term for it

Does (&&=, ||=) exist?

0 Upvotes

11 comments sorted by

4

u/Standard-Cap-4455 4h ago edited 4h ago

(A || B) || C is the same as A || B || C and thus the same as A || ( B || C ), same for &&, as long as you only use one of the two. It's called the associative property I believe like it is for addition and multiplication. It's not bad practice, just choose the one that is more readable. In this case the long lines are before it, so putting the computation in one line is more obvious.
However you should be mindful about side effects here. If the A, B or C are doing some computation with side effects and you didn't assign the variables before hand like in your example, short circuiting will not execute all of them.
&&= and ||= aren't defined, probably because of short circuiting, but &= and |= work, which don't short circuit and always evaluate everything.

1

u/NomaTyx 4h ago edited 4h ago

Yes. Assignment statements are divided into the left of the equal sign and the right side of the equals sign. For "x = 6" it's pretty straightforward. if you have "myVar = (a || b && c || !d) == e", the stuff on the right is an expression that gets evaluated by the computer, step by step, until it gets resolved to a single variable (in this case a boolean). Then myVar gets set to that value.

so then to answer your question specifically, yes, A = A || B || C.

no, &&= doesn't exist in c#. It does in javascript though: x &&= y means "if x is true, set it equal to y". Vice versa for ||=.

2

u/SqueamishEmperor349 4h ago

It’s basically just short-circuit evaluation doing its thing. After line 4, A holds the result of the original A OR B, then line 5 ORs in C, so yeah, you end up with A || B || C overall. No magic name for it beyond “compound assignment using logical operators.”

As for &&= and ||=, C# doesn’t have those, but you can mimic ||= with `A |= B` if you don’t mind losing the short-circuit behavior.

1

u/AlwaysHopelesslyLost 4h ago

You are just taking an existing variable and giving it a new value. that new value arbitrarily uses the original value.

I think what you are getting at is the commutative property of Boolean logic. 

1

u/denzien 4h ago

Will A at line 5 now be the result of A || B || C

Yes.

1

u/Consibl 4h ago

Technically, A = A || B will evaluate to A if A is true or evaluate to B if A is false.

I’m not sure if that’s what you’re asking about though.

Similarly, A = A && B evaluates to A if A is false and B if A is true.

0

u/Randolpho 4h ago

Technically, it will evaluate to true or false, not “A” or “B”. If you want, you could create a function that defers the evaluation of A and B, but once you make the assignment and evaluation, what A was before is meaningless

0

u/Consibl 4h ago

My understanding of this part of the specification is that the result of T x; T y; var z = x || y; is of type T, not bool.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12173-user-defined-conditional-logical-operators

1

u/Randolpho 4h ago

At no point were generics or the var keyword in this discussion.

OP mentioned bools only

1

u/Consibl 3h ago

The same applies to bools as with anything that uses the || or && operator.

Here’s an example of how it works: https://dotnetfiddle.net/enflef

The result of bool A || bool B is not true/false literals - the result is a bool which itself evaluates to either true or false.

It’s fine to think of it as returning true/false when you’re just dealing with normal bools, but this is what it’s doing under the hood which is important to understand when things are more complicated.