How to Check If a C# String Array Contains Multiple Values?

In this C# tutorial, I will show you how to check if a C# string array contains multiple values with different examples.

There are different methods to check if a C# string array contains multiple values.

  • Using foreach loop
  • Using LINQ
  • Using HashSet

Check If a C# String Array Contains Multiple Values

Let us check all the methods to check if a C# string array contains multiple values with examples.

1. Using foreach Loop

One of the most straightforward ways to check if an array contains multiple specific values is to use foreach loop in C#. Here’s an example in which we check if the array contains the names “John” and “Sarah” using C# code.

using System;

namespace StringArrayContainsMultipleValues
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define a string array containing some names.
            string[] names = { "John", "Emily", "Sarah", "Michael", "Rachel", "Samantha" };

            // Define an array containing the values we want to check for.
            string[] valuesToCheck = { "John", "Sarah" };

            // Declare a boolean variable to keep track of whether all values exist in the array.
            bool allValuesExist = true;

            // Loop through each value to check.
            foreach (string value in valuesToCheck)
            {
                // Use Array.Exists() to check if the current value exists in the array.
                if (!Array.Exists(names, element => element == value))
                {
                    // If one of the values does not exist, set allValuesExist to false and break out of the loop.
                    allValuesExist = false;
                    break;
                }
            }

            // Output the result.
            if (allValuesExist)
            {
                Console.WriteLine("The array contains all specified values.");
            }
            else
            {
                Console.WriteLine("The array does not contain all specified values.");
            }
        }
    }
}

Once you run the code you can see the output below:

Check If a C# String Array Contains Multiple Values

2. Using LINQ

We can also use Language Integrated Query (LINQ) to check if a C# string array contains multiple values. The All() LINQ method returns true if all elements satisfy a condition. Here’s an example:

using System.Linq;

string[] names = {"John", "Emily", "Sarah", "Michael"};
string[] valuesToCheck = {"John", "Sarah"};

bool allValuesExist = valuesToCheck.All(value => names.Contains(value));

if (allValuesExist)
{
    Console.WriteLine("The array contains all specified values.");
}
else
{
    Console.WriteLine("The array does not contain all specified values.");
}

3. Using HashSet

If you need to perform this check multiple times or against a large array, a HashSet could offer a more optimized solution, thanks to its constant-time look-up operation:

using System.Collections.Generic;

string[] names = {"John", "Emily", "Sarah", "Michael"};
HashSet<string> nameSet = new HashSet<string>(names);
string[] valuesToCheck = {"John", "Sarah"};
bool allValuesExist = true;

foreach (string value in valuesToCheck)
{
    if (!nameSet.Contains(value))
    {
        allValuesExist = false;
        break;
    }
}

if (allValuesExist)
{
    Console.WriteLine("The array contains all specified values.");
}
else
{
    Console.WriteLine("The array does not contain all specified values.");
}

You can see the output in the screenshot below

c# string array contains multiple values

Conclusion

In this tutorial, we explored three different ways to check if a C# string array contains multiple specific values:

  1. Using traditional looping structures with Array.Exists().
  2. Using LINQ methods for a more streamlined approach.
  3. Using HashSet for an optimized solution when dealing with large arrays or frequent checks.

You may also like: