C# Array - LastIndexOf() Method



The C# Array LastIndexOf() method is used to search for the specified element or object in an array and return the index of its last occurrence. This method is manly useful when we want to find the last position of a value in a one-dimensional array.

The one-dimensional Array is searched backward starting at the last element and ending at the first element.

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 LastIndexOf() method −

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

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

Array.LastIndexOf(Array, Object, Int32)

This syntax of LastIndexOf() method searches within a specified range of elements in the array −

Array.LastIndexOf(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 last occurrence of value in array, if found; otherwise, the lower bound of the array -1.

Example 1: Last Index of the Specified String

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

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

Output

Following is the output −

Last occurrence of 'Banana': 3

Example 2: Search Last Index From Specific Index

Let's create another example of the LastIndexOf() method to display the last index of the specified element starts from the last given position −

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

Output

Following is the output −

Index of '3' starting from last position 3: 2

Example 3: Search Last Index in a Range

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

using System;

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

Output

Following is the output −

Last Index of 'f': 5

Example 4: Search for a Missing Element

Let's see another example, the LastIndexOf() method. The LastIndexOf 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.LastIndexOf(values, 6.6);
      Console.WriteLine($"Last Index of 6.6: {index}");
   }
}

Output

Following is the output −

Last Index of 6.6: -1
csharp_array_class.htm
Advertisements