C# Array - IndexOf() Method



The C# Array IndexOf() method returns the index of its first occurrence in a one-dimensional array or a range of elements after searching for the specified object in the array.

Exceptions

There are following exception of this methods −

  • ArgumentNullException: When array is null.
  • RankException: When array is multidimensional.

Syntax

Following are the syntax of the C# Array IndexOf() method −

public static int IndexOf (Array array, object? value);

This syntax of IndexOf() method start searching form the 1st index to specified element.

Array.IndexOf(Array, Object, Int32)

This syntax of IndexOf() method searches within a specified range of elements in the array.

Array.IndexOf(Array, Object, Int32, Int32)

Parameters

This method accepts the following parameters −

  • array: It specifies the one-dimensional array to search.
  • object: The object to locate in array.

Return value

This method returns index of the first occurrence of value in array, if found; otherwise, the lower bound of the array -1.

Example 1: First Index of the Specified String

This is the basic example of the IndexOf() method to display the index of the specified element −

using System;

class Program {
   static void Main() {
      string[] fruits = {
         "Apple",
         "Banana",
         "Cherry",
         "Banana",
         "Grapes"
      };
      int index = Array.IndexOf(fruits, "Banana");
      Console.WriteLine($"First occurrence of 'Banana': {index}");
   }
}

Output

Following is the output −

First occurrence of 'Banana': 1

Example 2: Search From Specific Index

Let's create another example the IndexOf() method to print the index of the specified element starts from the given index −

using System;
class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 3, 5, 6 };
      int index = Array.IndexOf(numbers, 3, 3);
      Console.WriteLine($"Index of '3' starting from position 3: {index}");
   }
}

Output

Following is the output −

Index of '3' starting from position 3: 4

Example 3: Search In a Range

Let's see another example of theIndexOf() method, Here we specified the range to search a index of element within this range −

using System;

class Program {
   static void Main() {
      char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
      int index = Array.IndexOf(letters, 'f', 2, 4);
      Console.WriteLine($"Index of 'f' within range 2-5: {index}");
   }
}

Output

Following is the output −

Index of 'f' within range 2-5: 5

Example 4: Search for a Missing Element

Let's see another example, the IndexOf() method. The IndexOf display -1 if the element or object is not present in the array −

using System;

class Program {
   static void Main() {
      double[] values = { 1.1, 2.2, 3.3, 4.4, 5.5 };
      int index = Array.IndexOf(values, 6.6);
      Console.WriteLine($"Index of 6.6: {index}");
   }
}

Output

Following is the output −

Index of 6.6: -1
csharp_array_class.htm
Advertisements