There is no such operator in switch
statements. The switch
statement operates on a single variable which is a value type or a string. See:
The real problem in your example is that both in the switch
expression, and in case
labels, you are applying &&
to string
s. You cannot apply &&
to strings, it only works on booleans (unless you overload it and define a new function that does work on strings).
What you are trying to accomplish is probably to simultaneously check the values of two different variables with one switch
. This is impossible; switch
only checks one variable at a time. The solution is to use if
statements or a specialized CheckStrings(string s1, string s2)
method (which may or may not use if
statements).
In a comment you have expressed concerns with length. Observe:
private int retValue(string x, string y)
{
if (x == "abc" && y == "1") return 10;
if (x == "xyz" && y == "2") return 20;
throw new Exception("No return value defined for these two strings.")
}
Shorter, even if you discount the gains from skipping redundant break;
statements and putting return
s on the same line.