Can a switch statement be used with arrays in C#?

Nov 03, 2026

Leave a message

Ava Davis
Ava Davis
Ava is a product manager at Hebei Natcon. She is in charge of expanding our product line. With her forward - thinking and market insights, she continuously introduces new and improved products to meet the ever - changing industry needs.

Hey there! As a switch supplier, I often get asked all sorts of questions about switches and programming. One question that pops up quite a bit is, "Can a switch statement be used with arrays in C#?" Let's dive into this topic and see what's what.

First off, let's quickly go over what a switch statement is in C#. A switch statement is a control flow statement that allows you to select one of many code blocks to be executed based on the value of an expression. It's a handy way to handle multiple possible values in a more organized manner compared to a bunch of if - else statements.

Now, when it comes to using a switch statement with arrays in C#, things get a bit tricky. By default, a switch statement in C# is designed to work with integral types (like int, char), enumeration types, strings, and nullable versions of these types. Arrays, on the other hand, are reference types that hold a collection of elements.

The problem is that you can't directly use an array as the expression in a switch statement. For example, the following code won't work:

int[] myArray = { 1, 2, 3 };
switch (myArray)
{
    case int[] arr when arr.Length == 3:
        Console.WriteLine("Array has 3 elements");
        break;
    default:
        Console.WriteLine("Something else");
        break;
}

This code will result in a compilation error because the switch statement doesn't support arrays as the controlling expression. But don't worry, there are workarounds.

One way to achieve something similar is by using a property or a calculated value of the array. For instance, if you're interested in the length of the array, you can use the Length property in the switch statement.

int[] myArray = { 1, 2, 3 };
switch (myArray.Length)
{
    case 3:
        Console.WriteLine("Array has 3 elements");
        break;
    default:
        Console.WriteLine("Array has a different number of elements");
        break;
}

This approach works because the Length property returns an integer, which is a valid type for a switch statement.

H3C S5560XH3C S5735 Switch high quality

Another scenario could be when you want to check if an array contains a specific value. You can use LINQ (Language Integrated Query) to perform this check and then use the result in a switch statement.

int[] myArray = { 1, 2, 3 };
bool containsTwo = myArray.Contains(2);
switch (containsTwo)
{
    case true:
        Console.WriteLine("Array contains 2");
        break;
    case false:
        Console.WriteLine("Array does not contain 2");
        break;
}

Now, let's talk a bit about the switches we offer as a supplier. We have some great products like the H3C S5560X. This switch is known for its high - performance and reliability. It's suitable for a wide range of network environments, whether it's a small business or a large enterprise.

Another awesome product is the H3C S5735 Switch. It comes with advanced features that can help optimize your network traffic. It's designed to handle high - volume data transfer and provides excellent security features.

And then there's the H3C S1850. This is a great option for those looking for a cost - effective switch solution. It's easy to install and manage, making it a popular choice for small - scale networks.

In conclusion, while you can't directly use an array in a switch statement in C#, there are ways to work around it by using properties or calculated values of the array. If you're in the market for a reliable switch, we've got you covered with our great selection of products. Whether you're a developer looking to set up a network for your projects or a business owner in need of a robust network infrastructure, our switches can meet your needs.

If you're interested in learning more about our products or want to discuss a potential purchase, don't hesitate to reach out. We're here to help you find the perfect switch for your requirements.

References:

  • C# Programming Guide - Microsoft Docs
  • LINQ in C# - Microsoft Docs
Send Inquiry
Contact usif have any question

You can either contact us via phone, email or online form below. Our specialist will contact you back shortly.

Contact now!