Take the 2-minute tour ×

I read some articles from Stackoverflow.com, especially:

What's the most concise way to get the inverse of a Java boolean value?

Easiest way to flip a boolean value?

What's happens if I have three boolean variables ? I want to assign a true/false value using single line.

For example, test1 and test3 must be true and test2 must be false.

I used

test1 = test2 ^= test3 = true;   //true, false, true


test1 = test3 ^= test2 ^= true;

But it not good. The logic is not good.

I know that my question is simple but I have 6-7 boolean variables and I want to assign values using single line, if is possible.

It is possible ?

share|improve this question
While I am intrigued to find out whether it is possible, consider readability: why on earth would you want to do such a thing? –  Digitalex Aug 3 '12 at 11:24
What do you mean "because it is only for conditional or loop statements?" Could you post an example of this limitation? –  Michael Graczyk Aug 3 '12 at 11:24
@Digitalex: I asked if is possible. –  Snake Eyes Aug 3 '12 at 11:26
Why does this have to be on a single line? You could do test1 = test3 = ... = true; test2 = test4 = ... = false; which is only two lines. –  Lee Aug 3 '12 at 11:26
var test = new BitVector32(5); –  Hans Passant Aug 3 '12 at 12:14
show 5 more comments

2 Answers

up vote 0 down vote accepted
a = b = c = !(d = e = f = true);

This will set the first ones to false, and the second ones to true. (From D onwards.)

So in your case:

test2 = !(test1 = test3 = true);
share|improve this answer
add comment

You can do this:

test1 = !(test2 = !(test3 = true));

Although you should definitely split the assignments onto multiple lines:

test1 = true;
test2 = false;
test3 = true;

I cannot think of any place in c# in which you can do the former, but cannot do the latter.

share|improve this answer
add comment

Your Answer


