Array.LastIndexOf Method in C# | Set – 2

Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows:

  • LastIndexOf(Array, Object)
  • LastIndexOf(Array, Object, Int32)
  • LastIndexOf(Array, Object, Int32, Int32)
  • LastIndexOf<T>(T[], T)
  • LastIndexOf<T>(T[], T, Int32)
  • LastIndexOf<T>(T[], T, Int32, Int32)

Here, we will discuss the last three methods.

LastIndexOf<T>(T[], T) Method

This method searches for the specified object and returns the index of the last occurrence within the entire 1-D Array.

Syntax: public static int LastIndexOf<T> (T[] arr, T value);
Here, T is the type of the elements of the array.

Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array.

Return Value: If the object is found then the zero-based index of the last occurrence of value within the entire array is returned, otherwise, -1.

Exception: This method will give the ArgumentNullException if the array is null.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Array.LastIndexOf<T>(T[], T)
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // array elements
        string[] arr = { "ABC",
                         "EFG",
                         "GHI",
                         "LMO",
                         "STW",
                         "XYZ",
                         "QRS" };
  
        Console.WriteLine("Original Array");
  
        foreach(string g in arr)
        {
            Console.WriteLine("Original array" + g);
        }
  
        // here the object is STW
        Console.WriteLine("\nThe position of STW is "
                        Array.LastIndexOf(arr, "STW"));
          
    }
}

chevron_right


Output:

Original Array
Original arrayABC
Original arrayEFG
Original arrayGHI
Original arrayLMO
Original arraySTW
Original arrayXYZ
Original arrayQRS

The position of STW is 4

LastIndexOf<T>(T[], T, Int32) Method

This method searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.

Syntax: public static int LastIndexOf<T> (T[] arr, T value, int start);
Here, T is the type of the elements of the array.

Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array
start: It is the zero-based starting index of the backward search.

Exceptions:

  • ArgumentNullException: If the array is null.
  • ArgumentOutOfRangeException: If the “start” is outside the range of valid indexes for array.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Array.LastIndexOf<T>(T[], T, Int32)
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // array elements
        string[] arr = { "ABC",
                         "EFG",
                         "GHI",
                         "LMO",
                         "STW",
                         "XYZ",
                         "QRS" };
  
        Console.WriteLine("Original Array");
        foreach(string g in arr)
        {
            Console.WriteLine(g);
        }
  
        // Here the object is STW
        // the search starts from index 5
        Console.WriteLine("\nthe position of STW is " +
                     Array.LastIndexOf(arr, "STW", 5));
  
    }
}

chevron_right


Output:

Original Array
ABC
EFG
GHI
LMO
STW
XYZ
QRS

the position of STW is 4

LastIndexOf<T>(T[], T, Int32, Int32) Method

This method searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

Syntax: public static int LastIndexOf<T> (T[] arr, T value, int start, int count);

Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array.
start: It is the zero-based starting index of the backward search.
count: It is the number of elements in the section to search.

Return Value: This method will return zero-based index of the last occurrence of value within the range of elements in the array that contains the number of elements specified in the count and ends at start if found; otherwise, -1.

Exceptions:

  • ArgumentNullException: If the array is null.
  • ArgumentOutOfRangeException: If the start is outside the range of valid indexes for array or start and count do not specify a valid section in array.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Array.LastIndexOf(T[], T, 
// Int32, Int32)
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // array elements
        string[] arr = { "ABC",
                         "EFG",
                         "GHI",
                         "LMO",
                         "STW",
                         "XYZ",
                         "QRS" };
  
        Console.WriteLine("Original Array");
  
        foreach(string g in arr)
        {
            Console.WriteLine(g);
        }
  
         // here the object is STW
        // the search starts from index 5
        // the search happens bacward from 
        // index 5 to another 2 index
        Console.WriteLine("\nthe position of STW is "
                  Array.LastIndexOf(arr, "STW", 5, 2));
  
          
    }
}

chevron_right


Output:

Original Array
ABC
EFG
GHI
LMO
STW
XYZ
QRS

the position of STW is 4

Reference:



My Personal Notes arrow_drop_up

2nd year student of Information Technology JADAVPUR UNIVERSITY

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.