Phương thức Array.LastIndexOf() trong C# tìm kiếm đối tượng được chỉ định và trả về chỉ số của lần xuất hiện cuối cùng trong toàn bộ mảng.
Cú pháp
public static int LastIndexOf(T[] array, T val);
Tham số array là mảng một chiều, val là đối tượng cần tìm.
Ví dụ
using System;
public class Demo
{
public static void Main()
{
Console.WriteLine("Array elements...");
string[] arr = { "car", "bike", "truck", "bus" };
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
int lastIndex = Array.LastIndexOf(arr, "bus");
Console.WriteLine("Last Occurrence of element bus is at index = " + lastIndex);
}
}
Đầu ra
Array elements... car bike truck bus Last Occurrence of element bus is at index = 3