This C# tutorial is about a complete example of a “c# string array contains exact match”. Here I will show you how to check for an exact match in a C# string array.
C# String Array Contains Exact Match
When working with arrays in C#, it’s often necessary to determine whether a specific value exists within the array. Specifically, you might need to find out if the array contains an exact match for a particular string.
Let us check out various methods to check if a string array contains an exact match for a specific string in C#.
- Using the
Array.Exists
Method - Using LINQ (
System.Linq
) - Using a Loop for Manual Checking
1. Using the Array.Exists
Method
The Array.Exists
() method is a static method in the System.Array
class that allows you to determine whether an element in an array satisfies a certain condition. The method takes two arguments: the array to be searched and the predicate function that defines the condition.
Here is an example that uses the Array.Exists
method to check for an exact match in C#:
using System;
namespace StringArrayContainsExactMatch
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "John", "Emily", "Michael", "Sarah", "William" };
string targetName = "Sarah";
bool isFound = Array.Exists(usaNames, element => element == targetName);
if (isFound)
{
Console.WriteLine($"The array contains the name: {targetName}");
}
else
{
Console.WriteLine($"The array does not contain the name: {targetName}");
}
}
}
}
Once you run the C# code in a console application, you can see the output like below:

2. Using LINQ (System.Linq
)
Language Integrated Query (LINQ) is a feature in C# that provides a set of query operators to work with different data sources. You can use LINQ methods to check if a string array contains an exact match for a specific string. For this purpose, you can use the Any
method in C#.
Here’s how to do it:
using System;
using System.Linq;
namespace StringArrayContainsExactMatch
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "John", "Emily", "Michael", "Sarah", "William" };
string targetName = "Sarah";
bool isFound = usaNames.Any(element => element == targetName);
if (isFound)
{
Console.WriteLine($"The array contains the name: {targetName}");
}
else
{
Console.WriteLine($"The array does not contain the name: {targetName}");
}
}
}
}
3. Using a foreach Loop for Manual Checking
Although using built-in methods is often more efficient and convenient, you can also manually check each element in an array using a loop. This approach can be helpful when you want more control over the logic involved in the searching process.
Here is an example that uses a foreach
loop to check if a c# string array contains an exact match.
using System;
namespace StringArrayContainsExactMatch
{
class Program
{
static void Main(string[] args)
{
string[] usaNames = { "John", "Emily", "Michael", "Sarah", "William" };
string targetName = "Sarah";
bool isFound = false;
foreach (string name in usaNames)
{
if (name == targetName)
{
isFound = true;
break;
}
}
if (isFound)
{
Console.WriteLine($"The array contains the name: {targetName}");
}
else
{
Console.WriteLine($"The array does not contain the name: {targetName}");
}
}
}
}
After you execute the code, you can see the output below in the screenshot.

Conclusion
Checking if a string array contains an exact match for a specific string is a common operation in C#. You can accomplish this using different methods like Array.Exists
, LINQ’s Any
method, or manually checking each element using a loop. I hope you got an idea of how to check if a c# string array contains an exact match.
You may also like the following C# tutorials:
- How to reverse a string in C# using Linq?
- How to convert string to double with 2 decimals in C#.Net?
- How to generate random numbers in c#.net within a range without repeating?
- How to Convert Double to Decimal with 4 Places in C#?
- C# String Array Contains Wildcard
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…