This post will discuss how to find the index of an element in an array in C#.

The solution should either return the index of the first occurrence of the required element or -1 if it is not present in the array.

1. Using Array.IndexOf() method

The recommended solution is to use the Array.IndexOf() method that returns the index of the first occurrence of the specified element in this array.

Download  Run Code

2. Using Array.FindIndex() method

The Array.FindIndex() method returns the index of the first element that satisfies the provided predicate, or -1 if there is no such element.

Download  Run Code

3. Using Enumerable.Select() method

The System.Linq.Enumerable.Select() method projects each element of a sequence into a new form. The following code example demonstrates how we can use Select() to project over a sequence of values, and use both value and each element’s index to find the first occurrence of the specified element in this array.

Download  Run Code

 
We can avoid try-catch block by using FirstOrDefault() method instead of First():

Download  Run Code

A naive solution is to perform a linear search on the given array to determine whether the target element is present in the array.

Download  Run Code

That’s all about finding the index of an element in an array in C#.