C# – xor with 3 values

cconditionaloperators

I need to do an xor conditional between 3 values, ie i need one of the three values to be true but not more than one and not none.

I thought i could use the xor ^ operator for this but its not working as expected.

I expected that this would return false but it doesnt.
(true ^ true ^ true)

all other combinations seem to work as i expected.

When looking at the docs for the xor operator they only talk about comparing 2 values and i cant find anything on doing this for 3 or more values online.

Can anyone shed any light or suggest a simple way of doing this?

Best Answer

One way would be to convert the Boolean values to an integer, add the results, and compare to 1.