In this C# tutorial, I will show you, how to check if a string array contains a specific element in C# with a complete example.
When you’re working with arrays in C#, you might need to determine whether an array contains a particular element. In C#, the System.Array
class does not have a built-in Contains
method for checking the existence of an element, but the System.Linq
namespace provides an extension method that allows you to easily accomplish this task.
In this tutorial, I will explore how to use the Contains
() method to check if a string array contains a specific element in C#.
Check if a String Array Contains a Specific Element in C#
We can use the Contains() method to check if a string array contains a specific element in the C# array.
To use the Contains
in C# method, you’ll need to include the System.Linq
namespace in your code. Here’s how you can do it:
using System;
using System.Linq; // Required for the Contains method
namespace StringArrayContains
{
class Program
{
static void Main(string[] args)
{
// Initialize a string array with some names
string[] names = { "John", "Alice", "Bob", "Emily", "Sophia" };
// Check if the array contains the name "John"
bool containsJohn = names.Contains("John");
Console.WriteLine($"Array contains 'John': {containsJohn}"); // Output: Array contains 'John': True
// Check if the array contains the name "Michael"
bool containsMichael = names.Contains("Michael");
Console.WriteLine($"Array contains 'Michael': {containsMichael}"); // Output: Array contains 'Michael': False
// Check if the array contains any name from another array
string[] usaNames = { "George", "Thomas", "James" };
bool containsAnyUsaName = names.Intersect(usaNames).Any();
Console.WriteLine($"Array contains any name from the list of USA names: {containsAnyUsaName}"); // Output: Array contains any name from the list of USA names: False
}
}
}
In this example, we define a string array named names
and populate it with some example names. Then we use the Contains
method from the System.Linq
namespace to check if “John” and “Michael” are present in the array.
You can also see the output below:
Alternatives to Using Contains
If you do not want to use the System.Linq
namespace for some reason, there are other ways to accomplish the same thing. You can use a foreach
loop or the Array.Exists
method.
Using foreach Loop
We can also use foreach loop to check if a string array contains a specific element in C#.
bool containsJohn = false;
foreach (string name in names)
{
if (name == "John")
{
containsJohn = true;
break;
}
}
Using Array.Exists Method
We can also use the using Array.Exists() method to check if a string array contains a specific element in C#.
bool containsJohn = Array.Exists(names, element => element == "John");
Remember that the Contains
method is case-sensitive. If you want a case-insensitive search, you can convert the array and the search term to lower or upper case before performing the check:
bool containsJohnCaseInsensitive = names.Select(name => name.ToLower()).Contains("john".ToLower());
Conclusion
The Contains
method provided by the System.Linq
namespace offers a convenient way to check if a string array contains a specific element. While you can achieve the same result with loops or Array.Exists
, using Contains
makes your code cleaner and easier to understand. I hope you got an idea of how to check if a string array contains a specific element in C#.
You may like the following C# tutorials:
- How to Convert String to Double in C#.Net
- add trailing zeros to a string in C#.net
- How to Convert an Array to a Comma-Separated String in C#?
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…