The index of an element in an array in C# will be covered in this article.
If the requested element is absent from the array, the solution should return -1 instead of the index of the element’s first occurrence.
1. Making use of an array. IndexOf() method –
The method Array.IndexOf(), which returns the index of the first instance of the supplied element in this array, is the one that is advised as a fix.
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.IndexOf(array, item); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } Output: Element 4 is found at index 3
2. Making use of an array. the FindIndex() function –
The function FindIndex() provides the index of the first element that meets the given criteria, or -1 if no such element exists.
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.FindIndex(array, val => val.Equals(item)); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Output: Element 4 is found at index 3 */
3. Making use of Enumerable. the Select() function –
The System.Linq.Enumerable. The sequence’s individual elements are projected into new forms via the Select() function. The code snippet below shows how to use Select() to iterate over a list of values and discover the first instance of a given element in an array by using both the value and the index of each element.
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { try { return array .Select((element, index) => new KeyValuePair<T, int>(element, index)) .First(x => x.Key.Equals(item)).Value; } catch (InvalidOperationException) { return -1; } } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Output: Element 4 is found at index 3 */
4.Using FirstOrDefault() instead of First() will allow us to bypass the try-catch block:
using System; using System.Linq; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return array .Select((element, index) => new { element, index }) .FirstOrDefault(x => x.element.Equals(item)) ?. index ?? -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Output: Element 4 is found at index 3 */
5.Implementing a linear search –
To determine whether the target element is present in the array, a basic way is to run a linear search on the supplied array.
using System; using System.Collections.Generic; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < array.Length; i++) { if (comparer.Equals(array[i], item)) { return i; } } return -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Output: Element 4 is found at index 3 */
That’s all there is to locating an element’s index in an array in C#.