C# String Array Contains Exact Match Example

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#.

  1. Using the Array.Exists Method
  2. Using LINQ (System.Linq)
  3. 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:

c# string array contains exact match

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.

How to check c# string array contains exact match

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: