In this C# tutorial, I have explained, how to check if a C# string array contains multiple values with various methods and examples.
C# String Array Contains Multiple Values
A string array is an array that holds a collection of strings. Often, developers need to check if a string array contains one or more specific string values. In this complete tutorial, we’ll go over various ways to check if a C# string array contains multiple values.
Basic Approach: Using Array.Contains()
The most straightforward way to check if a string array contains a single value is by using the static method Array.Contains()
from the System
namespace in C#.
Here’s a basic example:
using System;
namespace StringArrayDemo
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "Alice", "Bob", "Charlie", "David" };
string nameToCheck = "Charlie";
if (Array.Exists(usaNames, element => element == nameToCheck))
{
Console.WriteLine($"{nameToCheck} is in the array.");
}
else
{
Console.WriteLine($"{nameToCheck} is not in the array.");
}
}
}
}
In this example, we used the Array.Exists()
method to check if the string “Charlie” exists in the string array usaNames
.
Once you run the code using Visual Studio, you can see the output below:
Checking Multiple Values
To extend this to multiple values, you could loop through the list of values you want to check and call Array.Contains()
for each one.
Here’s an example:
using System;
namespace StringArrayDemo
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "Alice", "Bob", "Charlie", "David" };
string[] namesToCheck = { "Alice", "Emily", "David" };
foreach (var name in namesToCheck)
{
if (Array.Exists(usaNames, element => element == name))
{
Console.WriteLine($"{name} is in the array.");
}
else
{
Console.WriteLine($"{name} is not in the array.");
}
}
}
}
}
Once you run the code using Visual Studio, you can check the output like the below screenshot.
Using LINQ for More Elegance
If you are familiar with LINQ (Language-Integrated Query), you can use it to make your code shorter and more readable.
Here’s how you can check if all the specified values are in the array in C#:
using System;
using System.Linq;
namespace StringArrayDemo
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "Alice", "Bob", "Charlie", "David" };
string[] namesToCheck = { "Alice", "Emily", "David" };
bool allExist = namesToCheck.All(name => usaNames.Contains(name));
if (allExist)
{
Console.WriteLine("All names are in the array.");
}
else
{
Console.WriteLine("Not all names are in the array.");
}
}
}
}
You can also check if any of the specified values exist in the array:
bool anyExist = namesToCheck.Any(name => usaNames.Contains(name));
if (anyExist)
{
Console.WriteLine("At least one name is in the array.");
}
else
{
Console.WriteLine("None of the names are in the array.");
}
Case Sensitivity
By default, the string comparison in C# is case-sensitive. To perform a case-insensitive check, you can specify the StringComparer.OrdinalIgnoreCase
in the Contains
method when using LINQ.
bool allExist = namesToCheck.All(name => usaNames.Contains(name, StringComparer.OrdinalIgnoreCase));
Summary
Checking if a string array contains one or more values is a common requirement in C# programming. While you can use basic loops and conditional statements to accomplish this, utilizing built-in methods like Array.Exists()
or LINQ queries can make your code more concise and readable. I hope you got an idea of how to check if a c# string array contains multiple values.
You may also like the following C# tutorials:
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…