7

How can I use && operator in switch case?

This is what i want to do:

private int  retValue()
{
    string x, y;
    switch (x && y)
    {
        case "abc" && "1":
            return 10;
            break;
         case "xyz" && "2":
             return 20;
            break;
    }
}

My problem is that "abc" and "1" are both of type string and the compiler gives me message that:

"operator && cannot be applied to string"

Fedor
  • 1,548
  • 3
  • 28
  • 38
RachitSharma
  • 567
  • 4
  • 11
  • 31

5 Answers5

9

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 strings. 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 returns on the same line.

Superbest
  • 25,318
  • 14
  • 62
  • 134
  • 2
    @Rambo-Raja No problem. I think this is a good question, because while it kind of doesn't make sense because "why would you think this is possible in the first place?", many people who are learning about "switch" (including me) immediately wonder about "fancy `switch`es" like this. – Superbest Feb 18 '14 at 18:02
  • 1
    @Rambo_Raja By the way, I have made non-trivial edits to the method example I gave (see revision details: https://stackoverflow.com/posts/21544951/revisions). – Superbest Feb 18 '14 at 18:10
5

Despite there is an accepted answer already...

To achieve logical AND in switch, it has to be done like this:

    switch(x + y)
    {
        case "abc1":
            return 10;
            break;
        case "xyz2":
            return 20;
            break;
    }

Which works.

For logical OR see zey answer.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
3

You mean like that ?

switch (value)
{
    case "abc":
    case "1":
        return 10;
    case "xyz":
    case "2":
        return 20;
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
zey
  • 5,939
  • 14
  • 56
  • 110
3

If you are using C# 8 and above below code snippet will yield the desired result. This is using pattern matching with expression future.

string x = "abc", y = "2";
var result = (x, y) switch
{ 
    ("abc","1") => 10,
    ("xyz","2") => 20,
    (_,_) => 0

};
Console.WriteLine($"Arguments : {x}, {y}, result : {result}");
lazer
  • 51
  • 3
1

switch statement can only be applied to integer values or constant expressions. If you want to check your conditions on string type variable, then you should use if-else-if structure.

Waqas Shabbir
  • 755
  • 1
  • 14
  • 34