Home > Software design >  Using a switch case statement with a List in c#
Using a switch case statement with a List in c#

Time:06-23

I am having trouble getting a Switch Case statement to work while switching on a list

    List<int> Test = new List<int>(){9, 6, 5};
    switch(Test)
    {
        case new List<int>(){9, 6, 5}:
            Console.Write("Yes");
        break;
            
        case new List<int>(){2, 4, 8}:
            Console.Write("No");
        break;
    }

I get the error "A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type".

Do switch statements just not work on collection types like List and Arrays?

CodePudding user response:

C#11 (as of writing still in preview) got you covered with the list pattern which might look something like this with your example (I can't fully test this, so take it with a grain of salt - @MatthewWatson posted a version without the case guards):

    List<int> Test = new List<int>(){9, 6, 5};
    switch(Test)
    {
        case var _ when Test is [9, 6, 5]:
            Console.Write("Yes");
        break;
            
        case var _ when Test is [2, 4, 8]:
            Console.Write("No");
        break;
    }

Until then you can do it using case guards like they did here which might look something like this:

    List<int> Test = new List<int>(){9, 6, 5};
    switch(Test)
    {
        // maybe implement it like some of the answers here https://stackoverflow.com/questions/876508/what-is-the-best-way-to-check-two-listt-lists-for-equality-in-c-sharp
        case var _ when Helper.AreEquivalent(Test, new List<int>(){9, 6, 5}):
            Console.Write("Yes");
        break;
            
        case var _ when Helper.AreEquivalent(Test, new List<int>(){2, 4, 8}):
            Console.Write("No");
        break;
    }

CodePudding user response:

You can use the new C# 11 pattern matching to solve this. You can enable it now in Visual Studio 2022 (in preview form) if you add the following to your project file (assuming SDK-style projects):

<LangVersion>preview</LangVersion>

Be aware that preview features might change before C# 11 is released, so you can't rely on this C# 11 feature until then.

Then you can write a switch statement like this:

switch (Test)
{
    case [9, 6, 5]: 
        Console.WriteLine("Yes"); 
        break;

    case [2, 4, 8]:
        Console.WriteLine("No");
        break;
}

Note that if you were trying to return a value rather than call a void-returning method, you could use a Switch Expression instead, which is nicer for some things.

Suppose you were returning a string instead:

Console.WriteLine(Test switch
{
    [9, 6, 5] => "Yes",
    [2, 4, 8] => "No",
    _         => "Other"
});

But of course if you're using a switch expression, you MUST provide a default value for it to return if no match for the cases is found.

CodePudding user response:

Switch statement only works on primitive data type i.e. int, string, bool etc. You can use the if else condition for this.

  • Related