Last Updated : 11 Jul, 2025
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:
Here, we will discuss the last three methods.
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:
// 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"));
}
}
Original Array Original arrayABC Original arrayEFG Original arrayGHI Original arrayLMO Original arraySTW Original arrayXYZ Original arrayQRS The position of STW is 4
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:
Example:
// 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));
}
}
Original Array ABC EFG GHI LMO STW XYZ QRS the position of STW is 4
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:
Example:
// 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 backward from
// index 5 to another 2 index
Console.WriteLine("\nthe position of STW is " +
Array.LastIndexOf(arr, "STW", 5, 2));
}
}
Original Array ABC EFG GHI LMO STW XYZ QRS the position of STW is 4
Reference:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4