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:

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

Conclusion
In this tutorial, we explored three different ways to check if a C# string array contains multiple specific values:
- Using traditional looping structures with
Array.Exists()
. - Using LINQ methods for a more streamlined approach.
- Using
HashSet
for an optimized solution when dealing with large arrays or frequent checks.
You may also like:
- Reverse a String in C# Using a For Loop
- How to convert string to double with 2 decimals in C#.Net?
- Convert a List to a Comma Separated String in C#
- How to Check If a C# String Array Contains a Partial String?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…