I'm very sure this question was already asked somewhere, but I couldn´t phrase my question right to get proper results. I am looking for a shorter, more effective way to write this condition
var test = new List<int>() { 1, 2 };
if (test.Contains(1) && test.Contains(2))
{
//Do stuff
}
else if (test.Contains(1))
{
//Do stuff
}
else if (test.Contains(2))
{
//Do stuff
}
I only came up with this idea, which is hard to read in my opinion
var result = test.Contains(1)
? test.Contains(2)
? "1 and 2"
: "1"
: "2";
(Note that this is just an example. This also applies to other cases, such as two Booleans which, when both true, should execute different code than if only one of them is true.
Edit1: As pointed out, the output of my idea is not equivalent to the first code snipped. Therefore it seems to be no valid solution.
Edit2: Further information to "Do stuff": All three "Do stuff" would execute completely different code, so that it is not possible to rephrase the condition to only use if(a) and if(b).
Hope this example helps:
string result = "";
var test = new List<int>() { 1, 2 };
if (test.Contains(1) && test.Contains(2))
{
//Do Something completely different
TriggerAnotherProcess();
}
else if (test.Contains(1))
{
result = "1";
}
else if (test.Contains(2))
{
result = "2";
}