Open In App

Array.LastIndexOf Method in C# | Set – 1

Last Updated : 12 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Array.LastIndexOf method is used to find the last matching element in a one-dimensional 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 discuss only the first three methods.

LastIndexOf(Array, Object)

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

Syntax: public static int LastIndexOf (Array arr, object value);

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

Return Value: If the index of value is found then, this method returns the index of the last occurrence of that value within the entire array. If not found then, the lower bound of the array minus 1.

Exceptions:

  • ArgumentNullException: If the array arr is null.
  • RankException: If the array arr is multidimensional.

Example 1:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object) method
using System;
  
class GFG {
  
    static void Main()
    {
        int[] arr = {1, 2, 6, 8, 6, 2};
  
        // search for "6" from end of the array
        // returns index 4
        int indx1 = Array.LastIndexOf(arr, 6);
          
        Console.WriteLine("First Index of 6 search"+
                     " from end found at " + indx1);
  
        // search for "2" from end of the array
        // returns index 5
        int indx2 = Array.LastIndexOf(arr, 2);
         
        // when the object is found then 
        // the search will stop searching
        Console.WriteLine("First Index of 2 search "+
                       "from end found at " + indx2);       
    }
}


Output:

First Index of 6 search from end found at 4
First Index of 2 search from end found at 5

Example 2:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are 
        // the indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("Python", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("C++", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
        int index1 = Array.LastIndexOf(arr, s1);
  
        Console.WriteLine("First Index of C# search"+
                     " from end found at " + index1);
  
        String s2 = "C++";
        int index2 = Array.LastIndexOf(arr, s2);
  
        Console.WriteLine("First Index of C++ search"+
                     " from end found at " + index2);
    }
}


Output:

First Index of C# search from end found at 5
First Index of C++ search from end found at 6

LastIndexOf(Array, Object, Int32)

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

Syntax: public static int LastIndexOf (Array arr, object value, int start);

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

Return Value: If the index of value is found then, this method returns the index of the last occurrence of that value within a range of elements in array “arr that extends from the first element to start. If not found then, the lower bound of the array minus 1.

Exceptions:

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

Example 1:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object,
// Int32) method
using System;
  
class GFG {
  
    static void Main()
    {
        int[] arr = {1, 2, 6, 8, 6, 2};
  
        // search for "6"
        // search start from index 3
        // so returns index 2
        int indx1 = Array.LastIndexOf(arr, 6, 3);
          
        Console.WriteLine("First Index of 6 search "+
                   "from index 3 found at " + indx1);
  
         // search for "2"
         // search start from index 2
        // so returns index 1
        int indx2 = Array.LastIndexOf(arr, 2, 2);
         
        Console.WriteLine("First Index of 2 search "+
                   "from index 2 found at " + indx2);
    }
}


Output:

First Index of 6 search from index 3 found at 2
First Index of 2 search from index 2 found at 1

Example 2:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object, 
// Int32) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are the
        // indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("Python", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("C++", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
  
        // the search start from index 4
        // so returns index 1
        int index1 = Array.LastIndexOf(arr, s1, 4);
         
        Console.WriteLine("First Index of C# search "+
                   "from index 4 found at " + index1);
  
        String s2 = "C++";
  
        // the search start from index 4
        // so returns index 2
        int index2 = Array.LastIndexOf(arr, s2, 3);
          
        Console.WriteLine("First Index of C++ search"+
                  " from index 3 found at " + index2);
    }
}


Output:

First Index of C# search from index 4 found at 1
First Index of C++ search from index 3 found at 2

LastIndexOf(Array, Object, Int32, Int32)

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

Syntax: public static int LastIndexOf (Array arr, object value, int start, int count);

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

Return Value: If the index of value is found then, this method returns the index of the last occurrence of that value within a range of elements in array “arr” that contains the number of elements specified in “count” and ends at “start” index. If not found then, the lower bound of the array minus 1.

Exceptions:

  • ArgumentNullException: If the array arr is null.
  • RankException: If the array arr is multidimensional.
  • ArgumentOutOfRangeException: If the “start” index is outside the range of valid indexes for array or “count” is less than zero, or “start” index and count do not specify a valid section in array.

Example 1:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object, Int32, Int32) method
using System;
  
class GFG {
  
    // Main Method
    static void Main()
    {
        int[] arr = {1, 5, 6, 2, 6, 8, 2};
  
        // search for "6"
        // search start from index 5 and
        // searches 2 indexes backward from index 5
        // at first index 4 is comes where the value is 6
        // so returns index 4
        int indx1 = Array.LastIndexOf(arr, 6, 5, 2);
          
        Console.WriteLine("First Index of 6 search from"+
                           " index 5 found at " + indx1);
  
        // search for "2"
        // search start from index 4 and
        // searches 2 indexes backward from index 4
        // at first index 3 is comes where the value is 2
        // so returns index 3
        int indx2 = Array.LastIndexOf(arr, 2, 4, 2);
          
        Console.WriteLine("First Index of 2 search from"+
                           " index 4 found at " + indx2);
    }
}


Output:

First Index of 6 search from index 5 found at 4
First Index of 2 search from index 4 found at 3

Example 2:




// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object,
// Int32, Int32) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are the 
        // indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("C++", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("Python", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
  
        // search for "C#"
        // search start from index 6 and
        // searches 3 indexes backward from index 6
        // at first index 5 is comes where the value is "C#"
        // so returns index 5
        int index1 = Array.LastIndexOf(arr, s1, 6, 3);
          
        Console.WriteLine("First Index of C# search from"+
                           " index 6 found at " + index1);
  
        String s2 = "C++";
  
        // search for "C++"
        // search start from index 5 and
        // searches 3 indexes backward from index 5
        // at first index 4 is comes where the value is "C++"
        // so returns index 4
        int index2 = Array.LastIndexOf(arr, s2, 5, 3);
         
        Console.WriteLine("First Index of C++ search from"+
                            " index 5 found at " + index2);
    }
}


Output:

First Index of C# search from index 6 found at 5
First Index of C++ search from index 5 found at 4

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads