Array.LastIndexOf Method in C# | Set – 2
Last Updated :
14 Aug, 2021
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:
csharp
// 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"));
}
}
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:
csharp
// 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));
}
}
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:
csharp
// 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));
}
}
Output: Original Array
ABC
EFG
GHI
LMO
STW
XYZ
QRS
the position of STW is 4
Reference:
Similar Reads
Array.LastIndexOf Method in C# | Set - 1
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: LastIndexO
8 min read
C# | Array.LastIndexOf<T>(T[], T) Method
Array.LastIndexOf<T>(T[], T) Method is used to search for the specified object. It returns the index of the last occurrence within the entire Array. Syntax: public static int LastIndexOf<T>(T[] array, T value); Parameters: array: It is a one-dimensional, zero-indexed array to search. val
2 min read
List FindLastIndex() Method in C# | Set -2
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) M
5 min read
C# | Array.FindLast() Method
This method is used to search for an element that matches the conditions defined by the specified predicate and returns the last occurrence within the entire Array. Syntax: public static T FindLast<T> (T[] array, Predicate<T> match); Parameters: array: It is the one-dimensional, zero-bas
3 min read
List FindLastIndex() Method in C# | Set -1
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) M
3 min read
C# | Array.ConstrainedCopy() Method
This method is used to copy a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. Syntax: public static void ConstrainedCop
9 min read
C# | Array.Find() Method
This method is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. Syntax: public static T Find (T[] array, Predicate<T> match); Here, T is the type of element of the array. Parameters: array: It
3 min read
C# String.IndexOf( ) Method | Set - 1
In C#, the IndexOf() method is a String method. Used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found. This method can be overloaded by passing different pa
6 min read
C# | ArrayList.InsertRange() Method
ArrayList.InsertRange(Int32, ICollection) Method in C# is used to insert the elements from a collection into the ArrayList at a specified index. That is the inserted element belongs to a collection(i.e. queue etc). Syntax: public virtual void InsertRange (int index, ICollection element); Parameters:
3 min read
Array.GetValue() Method in C# with Examples | Set â 2
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetVa
3 min read