c# - Operator To Return if one and ONLY One Argument is True -
i'm making if statement in c# return true if 1 of parameters true. i'll use ||
in example because that's closest thing can think of:
int = 1; int b = 2; int c = 3; if(a == 1 || b == 2) { console.log("the first statement true") } if(a == 1 || b == 3) { console.log("the second statement true") } if(a == 0 || b == 0 || c == 3) { console.log("the third statement true") } if(a == 1 || b == 0 || c == 3) { console.log("the fourth statement true") } //output: //the second statement true //the third statement true
again, think of ||
operator i'm looking for. such operator exist, or should define own boolean function?
for 2 expressions, can use xor:
if (a == 1 ^ b == 0)
for more two, like:
if (new[] { == 1, b == 0, c == 2 }.count(x => x) == 1)
that counts "true" elements of array constructed expressions, , checks count 1.
admittedly evaluate conditions first, , count of them if first 2 true (so final 1 irrelevant). if ends being expensive you, there more complex alternatives, i'd stick unless it's problem.
Comments
Post a Comment