Introduction
in this article, you can check if an array contains a specific element using the contents method of the System.Linq namespace. An array is a collection of elements of the same type stored in contiguous memory locations. Sometimes, you may need to check whether an array contains a specific element.
To check whether an array contains a specific element in C#, you can use the Contains method, which is part of the LINQ (Language Integrated Query) extension methods for the Enumerable interface. The Contains method returns a Boolean value indicating whether the specified element is present in the Array.
Here's the syntax for using the Contains method
Here's an example
public class Program
{
static void Main(string[] args)
{
// Create an array of integers
int[] numbers = { 2, 3, 4, 5, 6 };
// check if the array contains the number 3
bool containsfour = numbers.Contains(4);
//Print the result
Console.WriteLine("The array contains 3: " + containsfour);
}
}
In this statement, the Contains method is called on the numbers array, passing in the number 4 as the argument. The method returns a boolean value indicating whether the Array contains the specified element.
Note. that you need to include the System.Linq namespace at the top of your file in order to use the Contains method. An array is a collection of elements of the same type stored in contiguous memory locations. Each element in an array can be accessed using an index that starts at 0 for the first element and increments by 1 for each subsequent element. You can find a specific element in an array using various methods. Here are four common ways to do so in C#
- Using the IndexOf method.
- What is the specific element of an array?
- Using the Contains method.
- Using Existing value
Method 1. Using the IndexOf.
The Array.IndexOf method searches for a specific element in an array and returns its index position. Here is an example of how to use it
int[] num = { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(num, 3); //returns the index of the first occurrence of 3 in the array
This example has an integer array called numbers with five elements. We want to find the element's index with the value of 3. To do this, we call the Array.IndexOf method and pass in the Array as the first argument and the value we are searching for as the second argument. The method will return the index of the first occurrence of the value in the Array. In this case, the index variable will be set to 2 because three is at the third position in the Array.
If the value is not found in the Array, the Array.IndexOf method will return -1. For example
int[] num = { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(num, 6); // returns -1
In this statement, the value 6 is not present in the Array, so the Array.IndexOf method will return -1.
static void Main(string[] args)
{
// Create an array of integers
int[] numbers = { 20, 30, 40, 40, 50 };
// Find the index of the value 40
int index = Array.IndexOf(numbers, 40);
// Check if the value was found
if (index >= 0)
{
Console.WriteLine("The value 40 was found at index " + index);
}
else
{
Console.WriteLine("The value 40 was not found in the array");
}
// Find the index of the value 60 (which is not in the array)
index = Array.IndexOf(numbers, 60);
// Check if the value was found
if (index >= 0)
{
Console.WriteLine("The value 60 was found at index " + index);
}
else
{
Console.WriteLine("The value 60 was not found in the array");
}
Console.ReadLine();
}
}
In this, we create an array of integers and use the Array.IndexOf method to find the index of the value 30. We then check if the value was found and print a message to the Console.
We also use the Array.IndexOf method to find the index of the value 60, which is not in the Array. In this case, the method returns -1, so we print a different message to the Console.
The output of this program would be

Method 2. What is the specific element of an array?
In this statement, the specific element of an array is accessed using the index of the element within square brackets following the array variable name. For example, if you have an integer array named myArr, and you want to access the third element of the Array, you would write
int[] myArr = {1, 2, 3, 4, 5};
int thirdElement = myArr[2];

Join the conversation! Your thoughts help the community grow.